<!-- HTML header for doxygen 1.8.13-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Secure Sockets: Overview</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen_style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><a href="http://www.cypress.com/"><img alt="Logo" src="cypress_logo.png"/></a></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Secure Sockets</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('index.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div><div class="header">
  <div class="headertitle"><div class="title">Overview </div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p >Secure Sockets Library provides APIs to create software that can send and/or receive data over the network using sockets. This library supports both secure and non-secure sockets, and abstracts the complexity involved in directly using network stack and security stack APIs. This library supports both IPv4 and IPv6 addressing modes for UDP and TCP sockets.</p>
<h1><a class="anchor" id="section_features"></a>
Features and Functionality</h1>
<p >Features supported:</p><ul>
<li>Supports Wi-Fi and Ethernet connections</li>
<li>Supports non-secure TCP and UDP sockets</li>
<li>Secure TCP (TLS) socket communication using Mbed TLS/NetXSecure library</li>
<li>Supports both IPv4 and IPv6 addressing. Only link-local IPv6 addressing is supported</li>
<li>Supports UDP multicast and broadcast for both IPv4 and IPv6.</li>
<li>Thread-safe APIs</li>
<li>Provides APIs for both Client and Server mode operations</li>
<li>Supports both Synchronous and Asynchronous APIs for receiving data on socket</li>
<li>Asynchronous Server APIs for accepting client connections</li>
<li>Provides a socket-option API to configure send/receive timeout, callback for asynchronous mode, TCP keepalive parameters, certificate/key, and TLS extensions</li>
<li>Integrated with PSA Lib through PKCS interface to support secure client TCP (TLS) connection using the device certificate and device keys provisioned in the secure element</li>
</ul>
<h1><a class="anchor" id="section_integration"></a>
Quick Start</h1>
<ul>
<li>To use secure-sockets library with Wi-Fi kits, on FreeRTOS, lwIP, and Mbed TLS combination, the application should pull <a href="https://github.com/Infineon/wifi-core-freertos-lwip-mbedtls">wifi-core-freertos-lwip-mbedtls</a> library which will internally pull secure-sockets, wifi-connection-manager, FreeRTOS, lwIP, Mbed TLS and other dependent modules. To pull wifi-core-freertos-lwip-mbedtls create the following .mtb file in deps folder. wifi-core-freertos-lwip-mbedtls.mtb <div class="fragment"><div class="line">https:<span class="comment">//github.com/Infineon/wifi-core-freertos-lwip-mbedtls#latest-v1.X#$$ASSET_REPO$$/wifi-core-freertos-lwip-mbedtls/latest-v1.X</span></div>
</div><!-- fragment --></li>
<li>To use secure-sockets library with CYW955913EVK-01 kits on Threadx, NetXDuo, and NetXSecure combination, the application should pull <a href="https://github.com/Infineon/wifi-core-threadx-cat5">wifi-core-threadx-cat5</a> library which will internally pull secure-sockets and other dependent modules. To pull wifi-core-threadx-cat5 create the following .mtb file in deps folder. wifi-core-threadx-cat5.mtb <div class="fragment"><div class="line">https:<span class="comment">//github.com/Infineon/wifi-core-threadx-cat5#latest-v1.X#$$ASSET_REPO$$/wifi-core-threadx-cat5/latest-v1.X</span></div>
</div><!-- fragment --></li>
<li>To use secure-sockets library with Ethernet kits, on FreeRTOS, lwIP, and Mbed TLS combination, the application should pull <a href="https://github.com/Infineon/ethernet-core-freertos-lwip-mbedtls">ethernet-core-freertos-lwip-mbedtls</a> library which will internally pull secure-sockets, ethernet-connection-manager, FreeRTOS, lwIP, Mbed TLS and other dependent modules. To pull ethernet-core-freertos-lwip-mbedtls create the following .mtb file in deps folder. ethernet-core-freertos-lwip-mbedtls.mtb <div class="fragment"><div class="line">https:<span class="comment">//github.com/Infineon/ethernet-core-freertos-lwip-mbedtls#latest-v1.X#$$ASSET_REPO$$/ethernet-core-freertos-lwip-mbedtls/latest-v1.X</span></div>
</div><!-- fragment --></li>
<li>A set of pre-defined configuration files for FreeRTOS, lwIP, and Mbed TLS combination is bundled in wifi-core-freertos-lwip-mbedtls library for Wi-Fi kits and in ethernet-core-freertos-lwip-mbedtls library for Ethernet kits. The developer is expected to review the configuration and make adjustments. Also, a set of COMPONENTS must be defined in the code example project's Makefile for this library.<ul>
<li>See the "Quick Start" section in <a href="https://github.com/Infineon/wifi-core-freertos-lwip-mbedtls/blob/master/README.md">README.md</a> for Wi-Fi kits.</li>
<li>See the "Quick Start" section in <a href="https://github.com/Infineon/ethernet-core-freertos-lwip-mbedtls/blob/master/README.md">README.md</a> for Ethernet kits.</li>
</ul>
</li>
<li>The secure-sockets library disables all the debug log messages by default. To enable log messages, the application must perform the following:<ol type="1">
<li>Add the <code>ENABLE_SECURE_SOCKETS_LOGS</code> macro to the <em>DEFINES</em> in the code example's Makefile. The Makefile entry would look like as follows: <div class="fragment"><div class="line">DEFINES+=ENABLE_SECURE_SOCKETS_LOGS</div>
</div><!-- fragment --></li>
<li>Call the <code>cy_log_init()</code> function provided by the <em>cy-log</em> module. cy-log is part of the <em>connectivity-utilities</em> library. See *<a href="* https://Infineon.github.io/connectivity-utilities/api_reference_manual/html/group__logging__utils.html">connectivity-utilities library API documentation</a>. WPS is disabled by default. WPS uses Mbed TLS security stack. Enable the following components for WPS. <div class="fragment"><div class="line">COMPONENTS+=WPS MBEDTLS</div>
</div><!-- fragment --></li>
</ol>
</li>
</ul>
<h1><a class="anchor" id="section_platforms"></a>
Supported Platforms</h1>
<p >This library and its features are supported on the following Infineon MCUs:</p><ul>
<li><a href="https://www.cypress.com/documentation/development-kitsboards/psoc-6-wi-fi-bt-prototyping-kit-cy8cproto-062-4343w">PSoC&trade; 6 Wi-Fi Bluetooth&reg; prototyping kit (CY8CPROTO-062-4343W)</a></li>
<li><a href="https://www.cypress.com/documentation/development-kitsboards/psoc-62s2-wi-fi-bt-pioneer-kit-cy8ckit-062s2-43012">PSoC&trade; 62S2 Wi-Fi Bluetooth&reg; pioneer kit (CY8CKIT-062S2-43012)</a></li>
<li><a href="https://www.cypress.com/documentation/development-kitsboards/psoc-6-wifi-bt-pioneer-kit-cy8ckit-062-wifi-bt">PSoC&trade; 6 Wi-Fi Bluetooth&reg; pioneer kit (CY8CKIT-062-WiFi-BT)</a></li>
<li><a href="https://www.cypress.com/documentation/development-kitsboards/psoc-64-standard-secure-aws-wi-fi-bt-pioneer-kit-cy8ckit">PSoC&trade; 64S0S2 Wi-Fi Bluetooth&reg; pioneer kit (CY8CKIT-064S0S2-4343W)</a>*</li>
<li><a href="https://www.cypress.com/documentation/development-kitsboards/psoc-62s2-evaluation-kit-cy8ceval-062s2">PSoC&trade; 62S2 evaluation kit (CY8CEVAL-062S2-LAI-4373M2)</a></li>
<li><a href="https://www.cypress.com/documentation/development-kitsboards/cyw954907aeval1f-evaluation-kit">CYW954907AEVAL1F Evaluation Kit(CYW954907AEVAL1F)</a></li>
<li><a href="https://www.cypress.com/documentation/development-kitsboards/cyw943907aeval1f-evaluation-kit">CYW943907AEVAL1F Evaluation Kit(CYW943907AEVAL1F)</a></li>
<li><a href="https://www.cypress.com/documentation/development-kitsboards/psoc-62s2-evaluation-kit-cy8ceval-062s2">PSoC&trade; 62S2 evaluation kit (CY8CEVAL-062S2-MUR-43439M2)</a></li>
<li><a href="https://www.infineon.com/cms/en/product/evaluation-boards/kit_xmc72_evk/">XMC7200D-E272K8384 kit (KIT-XMC72-EVK)</a></li>
<li><a href="https://www.infineon.com/cms/en/product/evaluation-boards/kit_xmc72_evk/">XMC7200D-E272K8384 kit (KIT_XMC72_EVK_MUR_43439M2)</a></li>
<li><a href="https://www.cypress.com/documentation/development-kitsboards/psoc-62s2-evaluation-kit-cy8ceval-062s2">PSoC&trade; 62S2 evaluation kit (CY8CEVAL-062S2-CYW43022CUB)</a></li>
<li><a href="https://www.infineon.com/CYW955913EVK-01">CYW955913EVK-01 Wi-Fi Bluetooth&reg; Prototyping Kit (CYW955913EVK-01)</a></li>
<li><a href="https://www.infineon.com/cms/en/product/evaluation-boards/cy8ceval-062s2/">PSoC&trade; 62S2 evaluation kit (CY8CEVAL-062S2-CYW955513SDM2WLIPA)</a></li>
</ul>
<h1><a class="anchor" id="section_Send"></a>
Send and receive timeout values</h1>
<ul>
<li>The Secure Sockets Library configures the default send and receive timeout values to 10 seconds for a newly created socket. These can be changed using the <code>cy_socket_setsockopt</code> API function. To change the send timeout, use the <code>CY_SOCKET_SO_SNDTIMEO</code> socket option; similarly, for receive timeout, use the <code>CY_SOCKET_SO_RCVTIMEO</code> socket option. Adjust the default timeout values based on the network speed or use case.</li>
</ul>
<h1><a class="anchor" id="section_TCPIP"></a>
TCP/IP and security stacks</h1>
<ul>
<li>The secure sockets library has been designed to support different flavors of the TCP/IP stack or security stack. Currently, secure-sockets supports two combinations of TCP/IP stack and security stack.<ul>
<li>lwIP + Mbed TLS combination</li>
<li>NetXDuo + NetXSecure combination</li>
</ul>
</li>
<li>Any application that uses the secure sockets library with lwIP + Mbed TLS combination, must ensure that the following COMPONENTS are defined in the code example project's Makefile. To do so, add LWIP and MBEDTLS components to the Makefile. The Makefile entry would look like as follows: <div class="fragment"><div class="line">COMPONENTS+=LWIP MBEDTLS</div>
</div><!-- fragment --></li>
<li>Currently NetXDuo + NetXSecure combination is supported only on CYW955913EVK-01. This does not require addition of any COMPONENTS to the code example project's Makefile.</li>
<li>Applications using the Secure Sockets Library must include only the <em>cy_secure_sockets.h</em> file for non-secure connections. For secure connections, the application must include both <em>cy_secure_sockets.h</em> and <em>cy_tls.h</em> header files.</li>
</ul>
<h1><a class="anchor" id="section_Stack"></a>
Stack size</h1>
<ul>
<li>The default stack size of the secure sockets library is 6 KB (6*1024). To customize the stack size add the <code>SECURE_SOCKETS_THREAD_STACKSIZE</code> macro to the <code>DEFINES</code> in the code example's Makefile with the required stack size. The Makefile entry would look like as follows: <div class="fragment"><div class="line">DEFINES+=SECURE_SOCKETS_THREAD_STACKSIZE=8*1024</div>
</div><!-- fragment --></li>
</ul>
<h1><a class="anchor" id="section_Log"></a>
Log messages</h1>
<ul>
<li>The Secure Sockets Library disables all the debug log messages by default. Do the following to enable log messages:<ol type="1">
<li>Add <code>ENABLE_SECURE_SOCKETS_LOGS</code> macro to the <code>DEFINES</code> in the code example's Makefile. The Makefile entry would look like as follows: <div class="fragment"><div class="line">DEFINES+=ENABLE_SECURE_SOCKETS_LOGS</div>
</div><!-- fragment --></li>
<li>Call the <code>cy_log_init()</code> function provided by the <em>cy-log</em> module. cy-log is part of the <em>connectivity-utilities</em> library. See <a href="https://Infineon.github.io/connectivity-utilities/api_reference_manual/html/group__logging__utils.html">connectivity-utilities library API documentation</a> for cy-log details.</li>
</ol>
</li>
</ul>
<h1><a class="anchor" id="section_Validity"></a>
Validity period verification</h1>
<ul>
<li>The default Mbed TLS configuration provided by the <b>Wi-Fi core FreeRTOS lwIP mbedtls</b> library or <b>Ethernet core FreeRTOS lwIP mbedtls</b> library disables the validity period verification of the certificates. To perform this verification, enable <code>MBEDTLS_HAVE_TIME_DATE</code> in the <em>mbedtls_user_config.h</em><ul>
<li>See the <a href="https://github.com/Infineon/wifi-core-freertos-lwip-mbedtls/blob/master/configs/mbedtls_user_config.h">mbedtls_user_config.h</a> file in Wi-Fi core FreeRTOS lwIP mbedtls library for Wi-Fi kits.</li>
<li>See the <a href="https://github.com/Infineon/ethernet-core-freertos-lwip-mbedtls/blob/master/configs/mbedtls_user_config.h">mbedtls_user_config.h</a> file in Ethernet core FreeRTOS lwIP mbedtls library for Ethernet kits</li>
</ul>
</li>
<li>Ensure that the system time is set prior to the <a class="el" href="group__group__secure__sockets__functions.html#ga2e07189d60c7e6a130302d2e2db19653">cy_socket_connect()</a> function call. To set the system time, get the time from the NTP server and set the system's RTC time using cyhal_rtc_init(), cyhal_rtc_write() and cy_set_rtc_instance() functions. See the <a href="https://github.com/Infineon/clib-support/blob/master/README.md#time-support-details">Time Support Details</a> for reference. See the <a class="el" href="index.html#snip12">Code Snippet 12: Get the current time from the NTP server - Send an NTP request packet to the NTP server, and receive an NTP reply packet.</a> to get the time from NTP server.</li>
</ul>
<h1><a class="anchor" id="section_PKCS"></a>
PKCS/Non-PKCS mode</h1>
<ul>
<li>Secure sockets library can be built using PKCS and Non-PKCS mode on secure platform such as CY8CKIT-064S0S2-4343W. When Secure sockets library is built with PKCS flow, the certificates and keys can be provisioned in the secure element of the platform, and the provisioned certificates and keys will be read/used by secure sockets library while establishing the TLS connection with the peer. On the other hand, in non-PKCS mode, the certificates/keys will be passed from the application stored in flash/RAM.</li>
</ul>
<h2><a class="anchor" id="subsection_non_PKCS"></a>
Non-PKCS mode</h2>
<ul>
<li>Provision the kit. See <a href="https://community.cypress.com/t5/Resource-Library/Provisioning-Guide-for-the-Cypress-CY8CKIT-064S0S2-4343W-Kit/ta-p/252469">Device provisioning steps</a>.</li>
<li>Add <code>CY_TFM_PSA_SUPPORTED</code> and <code>TFM_MULTI_CORE_NS_OS</code> to the <code>DEFINES</code> in the application's Makefile. The Makefile entry would look like as follows: <div class="fragment"><div class="line">DEFINES+=CY_TFM_PSA_SUPPORTED TFM_MULTI_CORE_NS_OS</div>
</div><!-- fragment --></li>
</ul>
<h2><a class="anchor" id="subsection_PKCS_mode"></a>
PKCS mode</h2>
<h3><a class="anchor" id="subsection_PKCS_secure_kit"></a>
Secure Kits</h3>
<p >Secure kits will have inbuilt secure element to hold secret information like private key which can be used for the PKCS authentication purposes.</p>
<h3><a class="anchor" id="subsection_CYW955913EVK"></a>
1. CYW955913EVK-01 Kit</h3>
<p >CYW955913EVK-01 has inbuilt secure element which holds the unique private key of the device. Certificate shall be generated using the CSR from the device.</p><ul>
<li>Generate the device certificate: The device certificate can be generated using edgeprotect tool. Follow the below steps to generate the device certificate<ol type="1">
<li>Install edgeprotect tool. Skip this step if the tool is already installed.</li>
<li>Configure the connected serial port of the kit <div class="fragment"><div class="line">edgeprotecttools serial-config --protocol uart --hwid &lt;COM PORT&gt;</div>
</div><!-- fragment --></li>
<li>Initialize edgeprotect project <div class="fragment"><div class="line">edgeprotecttools -t cyw559xx init</div>
</div><!-- fragment --></li>
<li>Retrieve the Certificate Signing Request (CSR) from the device <div class="fragment"><div class="line">edgeprotecttools -t cyw559xx get-csr -o device.der</div>
</div><!-- fragment --></li>
<li>Copy root CA key and certificate to the project directory, update the <code>serial_number</code> and <code>subject</code> fields in <code>certs\x509cert.json</code> and use the below command to generate device certificate. <div class="fragment"><div class="line">edgeprotecttools x509-cert --config certs/x509cert.json --csr device.der --key keys/rootKey.pem --ca-cert keys/rootCert.pem -o device_certificate.pem</div>
</div><!-- fragment --> Note: If a different enviroment or tool will be used to generate the device certificate from CSR, the above step can be skipped.</li>
</ol>
</li>
</ul>
<h4><a class="anchor" id="subsubsection_Dependencies_CYW955913EVK"></a>
Dependencies</h4>
<p >The secure sockets library depends on the other libraries for PKCS support. Ensure that the following libraries are pulled in by creating the following .mtb files:</p><ul>
<li>aws-iot-device-sdk-embedded-C.mtb : <a href="https://github.com/aws/aws-iot-device-sdk-embedded-C/#202103.00#$$ASSET_REPO$$/aws-iot-device-sdk-embedded-C/202103.00">https://github.com/aws/aws-iot-device-sdk-embedded-C/#202103.00#$$ASSET_REPO$$/aws-iot-device-sdk-embedded-C/202103.00</a></li>
</ul>
<h4><a class="anchor" id="subsubsection_pull_CYW955913EVK"></a>
Pull required libraries and enable PKCS mode</h4>
<ul>
<li>Execute the <code>make getlibs</code> command to pull the required libraries created as .mtb.</li>
<li>Add the <code>CY_SECURE_SOCKETS_PKCS_SUPPORT</code> macro to the <code>DEFINES</code> in the code example's Makefile. The Makefile entry would look like as follows: <div class="fragment"><div class="line">DEFINES+= CY_SECURE_SOCKETS_PKCS_SUPPORT</div>
</div><!-- fragment --></li>
</ul>
<h3><a class="anchor" id="subsection_4343W"></a>
2. CY8CKIT-064S0S2-4343W</h3>
<p >CY8CKIT-064S0S2-4343W kits will have inbuilt secure element to store the keys and certificates which can be provisioned into the device.</p><ul>
<li>Provision the kit. See <a href="https://community.cypress.com/t5/Resource-Library/Provisioning-Guide-for-the-Cypress-CY8CKIT-064S0S2-4343W-Kit/ta-p/252469">Device provisioning steps</a>.</li>
<li>Provision device certificate/RootCA certificate. Add/modify the respective policy *.json* file with the device and RootCA certificate path to be provisioned to the secured element as follows, and then re-provision the kit: <div class="fragment"><div class="line"><span class="stringliteral">&quot;provisioning:&quot;</span></div>
<div class="line">{</div>
<div class="line">  <span class="stringliteral">&quot;chain_of_trust&quot;</span>: [<span class="stringliteral">&quot;../certificates/device_cert.pem&quot;</span>, <span class="stringliteral">&quot;../certificates/rootCA.pem&quot;</span>]</div>
<div class="line">},</div>
</div><!-- fragment --></li>
</ul>
<h4><a class="anchor" id="subsubsection_Dependencies_4343W"></a>
Dependencies</h4>
<p >The secure sockets library depends on the other libraries for PKCS support. Ensure that the following libraries are pulled in by creating the following .mtb files:</p><ul>
<li>aws-iot-device-sdk-embedded-C.mtb : <a href="https://github.com/aws/aws-iot-device-sdk-embedded-C/#202103.00#$$ASSET_REPO$$/aws-iot-device-sdk-embedded-C/202103.00">https://github.com/aws/aws-iot-device-sdk-embedded-C/#202103.00#$$ASSET_REPO$$/aws-iot-device-sdk-embedded-C/202103.00</a></li>
<li>freertos-pkcs11-psa.mtb : <a href="https://github.com/Linaro/freertos-pkcs11-psa/#80292d24f4978891b0fd35feeb5f1d6f6f0fff06#$$ASSET_REPO$$/freertos-pkcs11-psa/master">https://github.com/Linaro/freertos-pkcs11-psa/#80292d24f4978891b0fd35feeb5f1d6f6f0fff06#$$ASSET_REPO$$/freertos-pkcs11-psa/master</a></li>
</ul>
<h3><a class="anchor" id="subsubsection_pull_4343W"></a>
Pull required libraries and enable PKCS mode</h3>
<ul>
<li>Execute the <code>make getlibs</code> command to pull the required libraries created as .mtb.</li>
<li>Add the <code>CY_TFM_PSA_SUPPORTED</code>, <code>TFM_MULTI_CORE_NS_OS</code> and <code>CY_SECURE_SOCKETS_PKCS_SUPPORT</code> macros to the <code>DEFINES</code> in the code example's Makefile. The Makefile entry would look like as follows: <div class="fragment"><div class="line">DEFINES+=CY_TFM_PSA_SUPPORTED TFM_MULTI_CORE_NS_OS CY_SECURE_SOCKETS_PKCS_SUPPORT</div>
</div><!-- fragment --></li>
</ul>
<h3><a class="anchor" id="subsubsection_Trusted"></a>
Trusted firmware library include path</h3>
<ul>
<li>To compile the FreeRTOS PKCS PSA integration library, add the trusted firmware library include path before the MBEDTLS library include path. Add the following lines to the Makefile. <div class="fragment"><div class="line">INCLUDES=$(SEARCH_trusted-firmware-m)/COMPONENT_TFM_NS_INTERFACE/include</div>
<div class="line">INCLUDES+=./libs/trusted-firmware-m/COMPONENT_TFM_NS_INTERFACE/include</div>
</div><!-- fragment --></li>
</ul>
<h3><a class="anchor" id="subsection_PKCS_non_secure_kit"></a>
Non-Secure Kits</h3>
<p >The non-secure kits can also support the key and certificate storage in separate hardware like optiga. Optiga PKCS11 support is enabled in secure sockets.</p>
<h4><a class="anchor" id="subsubsection_Dependencies_optiga"></a>
Dependencies</h4>
<p >The secure sockets library depends on the other libraries for PKCS support. Ensure that the following libraries are pulled in by creating the following .mtb files:</p><ul>
<li>aws-iot-device-sdk-embedded-C.mtb : <a href="https://github.com/aws/aws-iot-device-sdk-embedded-C/#202103.00#$$ASSET_REPO$$/aws-iot-device-sdk-embedded-C/202103.00">https://github.com/aws/aws-iot-device-sdk-embedded-C/#202103.00#$$ASSET_REPO$$/aws-iot-device-sdk-embedded-C/202103.00</a></li>
<li>optiga-trust-m.mtb : <a href="https://github.com/Infineon/optiga-trust-m#release-v4.0.3#$$ASSET_REPO$$/optiga-trust-m/release-v4.0.3">https://github.com/Infineon/optiga-trust-m#release-v4.0.3#$$ASSET_REPO$$/optiga-trust-m/release-v4.0.3</a></li>
</ul>
<h3><a class="anchor" id="subsubsection_pull_optiga"></a>
Pull required libraries and enable PKCS mode</h3>
<ul>
<li>Execute the <code>make getlibs</code> command to pull the required libraries created as .mtb.</li>
<li>Add <code>OPTIGA</code> in <code>COMPONENTS</code> <div class="fragment"><div class="line">COMPONENT+=OPTIGA</div>
</div><!-- fragment --></li>
<li>Add the Optiga PAL interface as per the platform. Use <code>PSOC6_FREERTOS</code> for PSOC6 with FREERTOS. The Makefile entry would look like as follows: <div class="fragment"><div class="line">COMPONENT+=PSOC6_FREERTOS</div>
</div><!-- fragment --></li>
<li>Add <code>OPTIGAFLAGS</code> with the configuration file for Optiga. A pre-defined configuration file optiga_lib_config_mtb.h is bundled with the secure sockets library. To change the default configuration for PKCS11, copy the optiga_config.h file from the secure sockets library to the top-level application directory, and then modify it. <div class="fragment"><div class="line">OPTIGAFLAGS=OPTIGA_LIB_EXTERNAL=<span class="stringliteral">&#39;&quot;optiga_config.h&quot;&#39;</span></div>
</div><!-- fragment --></li>
<li>Add the <code>CY_SECURE_SOCKETS_PKCS_SUPPORT</code> and <code>OPTIGAFLAGS</code> macros to the <code>DEFINES</code> in the code example's Makefile. The Makefile entry would look like as follows: <div class="fragment"><div class="line">DEFINES+= $(OPTIGAFLAGS) CY_SECURE_SOCKETS_PKCS_SUPPORT</div>
</div><!-- fragment --></li>
</ul>
<h3><a class="anchor" id="subsubsection_Configuration"></a>
Configuration for PKCS11</h3>
<p >Pre-defined configuration file core_pkcs11_config.h is bundled with secure sockets library. If you need to change default configuration for PKCS11, copy core_pkcs11_config.h file from secure sockets library to top-level code example directory and then modify it.</p>
<h4><a class="anchor" id="subsubsection_Configuration_Secure_kit"></a>
Secure Kits</h4>
<h4><a class="anchor" id="subsubsection_Configuration_CYW955913EVK"></a>
1. CYW955913EVK-01 Kit</h4>
<p >CYW955913EVK-01 Kit PKCS11 only supports ciphers having SHA-256 hashing algorithm.</p>
<h4><a class="anchor" id="subsubsection_Configuration_4343W"></a>
2. CY8CKIT-064S0S2-4343W</h4>
<p ><a href="https://github.com/Linaro/freertos-pkcs11-psa/">FreeRTOS PSA PKCS11</a> library supports only SHA-256 hashing algorithm. So the application should chose the cipher suite list compatible for SHA-256. To chose the cipher suite list(compatible for SHA-256), application need to copy mbedtls_user_config.h file from libs/wifi-core-freertos-lwip-mbedtls/configs to root folder and add required cipher suites to the <code>MBEDTLS_SSL_CIPHERSUITES</code> macro.</p>
<h4><a class="anchor" id="subsubsection_Configuration_Non_Secure_kit"></a>
Non-Secure Kits</h4>
<ul>
<li>The secure sockets will use 3 OID information from the optiga chip for TLS connection. Secure sockets uses the below macros (default OID value in brackets) correspoding to those OIDs to fetch information. To change the default configuration, add these defines in the makefile with new values.</li>
<li>Device private key : LABEL_DEVICE_PRIVATE_KEY_FOR_TLS (0xE0F0)</li>
<li>Device certificate : LABEL_DEVICE_CERTIFICATE_FOR_TLS (0xE0E0)</li>
<li>Root certificate : LABEL_ROOT_CERTIFICATE (0xE0E8)</li>
</ul>
<p >Note : Optiga-trust-m has a limitation on the maximum size of Public Key Certificate/Device Certificate. Using a certificate of a size greater than 1728 bytes may result in erroneous outputs.</p>
<p >To use secure-sockets library for FreeRTOS, lwIP, and Mbed TLS, pull <a href="https://github.com/Infineon/wifi-core-freertos-lwip-mbedtls">wifi-core-freertos-lwip-mbedtls</a> library which will internally pull secure-sockets, wifi-connection-manager, FreeRTOS, lwIP, mbed TLS and other dependent modules.</p>
<h1><a class="anchor" id="section_code_snippet"></a>
Code Snippets</h1>
<h2><a class="anchor" id="snip1"></a>
Code Snippet 1: Create TCP Socket</h2>
<p >This code snippet demonstrates how to initialize and create a TCP socket using an IPv4 address domain. </p><div class="fragment"><div class="line"><span class="keywordtype">void</span> snippet_create_socket()</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* TCP socket handle. */</span></div>
<div class="line">    <a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> socket_handle;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a TCP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gac0610ba6e8fe01ccbaacf89145f9285a">CY_SOCKET_TYPE_STREAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga1c6d23edbc333c85d3e7a8702dcf22a1">CY_SOCKET_IPPROTO_TCP</a>, &amp;socket_handle);</div>
<div class="line">}</div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_ga30bbbf20a277e2a7e622df504b014a4b"><div class="ttname"><a href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a></div><div class="ttdeci">cy_rslt_t cy_socket_create(int domain, int type, int protocol, cy_socket_t *handle)</div><div class="ttdoc">Creates a new socket.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_ga49a49b4e3b4293aa3855f47ce831ebe2"><div class="ttname"><a href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a></div><div class="ttdeci">cy_rslt_t cy_socket_init(void)</div><div class="ttdoc">Does general allocation and initialization of resources needed for the library.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_ga1c6d23edbc333c85d3e7a8702dcf22a1"><div class="ttname"><a href="group__group__secure__sockets__macros.html#ga1c6d23edbc333c85d3e7a8702dcf22a1">CY_SOCKET_IPPROTO_TCP</a></div><div class="ttdeci">#define CY_SOCKET_IPPROTO_TCP</div><div class="ttdoc">Protocol option for cy_socket_create() - TCP.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:59</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_gac0610ba6e8fe01ccbaacf89145f9285a"><div class="ttname"><a href="group__group__secure__sockets__macros.html#gac0610ba6e8fe01ccbaacf89145f9285a">CY_SOCKET_TYPE_STREAM</a></div><div class="ttdeci">#define CY_SOCKET_TYPE_STREAM</div><div class="ttdoc">Type parameter for cy_socket_create() - Byte-stream.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:54</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_gacc301fb73845cb915cfacffc1b7d0c23"><div class="ttname"><a href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a></div><div class="ttdeci">#define CY_SOCKET_DOMAIN_AF_INET</div><div class="ttdoc">Domain option for cy_socket_create() - IPv4 internet protocols.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:47</div></div>
<div class="ttc" id="agroup__group__secure__sockets__typedefs_html_ga12b5aa294d6c930ba50ab730d39ed511"><div class="ttname"><a href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a></div><div class="ttdeci">void * cy_socket_t</div><div class="ttdoc">Socket handle.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:124</div></div>
</div><!-- fragment --><h2><a class="anchor" id="snip2"></a>
Code Snippet 2: Create Secure TCP Socket</h2>
<p >This code snippet demonstrates how to initialize and create a TCP socket using an IPv4 address domain, and set the TLS credentials to be used for securing the socket communication. </p><div class="fragment"><div class="line"><span class="keywordtype">void</span> snippet_create_secure_socket()</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* TCP socket handle */</span></div>
<div class="line">    <a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> socket_handle;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable to store the TLS identity (certificate and private key).*/</span></div>
<div class="line">    <span class="keywordtype">void</span> *tls_identity;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* TLS authentication mode. Setting the CY_SOCKET_TLS_VERIFY_REQUIRED authentication mode</span></div>
<div class="line"><span class="comment">    requires a successful TLS handshake in order to establish a secure socket communication.*/</span></div>
<div class="line">    <a class="code hl_enumeration" href="group__group__secure__sockets__enums.html#gaf0599f554a1154ae83b4f587520fa705">cy_socket_tls_auth_mode_t</a> tls_auth_mode = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#ggaf0599f554a1154ae83b4f587520fa705a68f0cd442bb9f64416a6351f6d8c5a78">CY_SOCKET_TLS_VERIFY_REQUIRED</a>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize with your TLS certificate. Must include the PEM header and footer.*/</span></div>
<div class="line">    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> tls_cert[] = <span class="stringliteral">&quot;-----REPLACE WITH TLS CERTIFICATE FILE-----\n&quot;</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize with private key corresponding to the TLS certificate used.</span></div>
<div class="line"><span class="comment">     * Must include the PEM header and footer. */</span></div>
<div class="line">    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> private_key[] = <span class="stringliteral">&quot;-----REPLACE WITH PRIVATE KEY-----\n&quot;</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* TLS certificate length and private key length. */</span></div>
<div class="line">    <span class="keyword">const</span> uint32_t cert_len = strlen( tls_cert );</div>
<div class="line">    <span class="keyword">const</span> uint32_t pkey_len = strlen( private_key );</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a new secure TCP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gac0610ba6e8fe01ccbaacf89145f9285a">CY_SOCKET_TYPE_STREAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga1114a6ecf098cf499e2c9ad3d651efc3">CY_SOCKET_IPPROTO_TLS</a>, &amp;socket_handle);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a TCP client identity using the SSL certificate and private key. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__cy__tls__functions.html#ga3d2408681bbcea3b869cb5ae21cd0d5e">cy_tls_create_identity</a>(tls_cert, cert_len, private_key, pkey_len, &amp;tls_identity);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Set the TCP socket to use the TLS identity. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(socket_handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga87878a7761a845f17f24f5e52c73f61f">CY_SOCKET_SOL_TLS</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gafa0ac839774a33feeb037dbb0c449cc3">CY_SOCKET_SO_TLS_IDENTITY</a>, tls_identity, <span class="keyword">sizeof</span>(tls_identity));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Set the TLS authentication mode. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(socket_handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga87878a7761a845f17f24f5e52c73f61f">CY_SOCKET_SOL_TLS</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga9ce4ab8fca2ff7306f05f7433835f394">CY_SOCKET_SO_TLS_AUTH_MODE</a>, &amp;tls_auth_mode, <span class="keyword">sizeof</span>(<a class="code hl_enumeration" href="group__group__secure__sockets__enums.html#gaf0599f554a1154ae83b4f587520fa705">cy_socket_tls_auth_mode_t</a>));</div>
<div class="line">}</div>
<div class="ttc" id="agroup__group__cy__tls__functions_html_ga3d2408681bbcea3b869cb5ae21cd0d5e"><div class="ttname"><a href="group__group__cy__tls__functions.html#ga3d2408681bbcea3b869cb5ae21cd0d5e">cy_tls_create_identity</a></div><div class="ttdeci">cy_rslt_t cy_tls_create_identity(const char *certificate_data, const uint32_t certificate_len, const char *private_key, uint32_t private_key_len, void **tls_identity)</div><div class="ttdoc">Creates an identity structure from the supplied certificate and private key.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__enums_html_gaf0599f554a1154ae83b4f587520fa705"><div class="ttname"><a href="group__group__secure__sockets__enums.html#gaf0599f554a1154ae83b4f587520fa705">cy_socket_tls_auth_mode_t</a></div><div class="ttdeci">cy_socket_tls_auth_mode_t</div><div class="ttdoc">Options for socket option CY_SOCKET_SO_TLS_AUTH_MODE.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:94</div></div>
<div class="ttc" id="agroup__group__secure__sockets__enums_html_ggaf0599f554a1154ae83b4f587520fa705a68f0cd442bb9f64416a6351f6d8c5a78"><div class="ttname"><a href="group__group__secure__sockets__enums.html#ggaf0599f554a1154ae83b4f587520fa705a68f0cd442bb9f64416a6351f6d8c5a78">CY_SOCKET_TLS_VERIFY_REQUIRED</a></div><div class="ttdeci">@ CY_SOCKET_TLS_VERIFY_REQUIRED</div><div class="ttdoc">Peer must present a valid certificate; handshake is aborted if verification failed (default authentic...</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:97</div></div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_ga02e453d715d77075c59d751f6b07ae8f"><div class="ttname"><a href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a></div><div class="ttdeci">cy_rslt_t cy_socket_setsockopt(cy_socket_t handle, int level, int optname, const void *optval, uint32_t optlen)</div><div class="ttdoc">Sets a particular socket option.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_ga1114a6ecf098cf499e2c9ad3d651efc3"><div class="ttname"><a href="group__group__secure__sockets__macros.html#ga1114a6ecf098cf499e2c9ad3d651efc3">CY_SOCKET_IPPROTO_TLS</a></div><div class="ttdeci">#define CY_SOCKET_IPPROTO_TLS</div><div class="ttdoc">Protocol option for cy_socket_create() - TLS.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:61</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_ga87878a7761a845f17f24f5e52c73f61f"><div class="ttname"><a href="group__group__secure__sockets__macros.html#ga87878a7761a845f17f24f5e52c73f61f">CY_SOCKET_SOL_TLS</a></div><div class="ttdeci">#define CY_SOCKET_SOL_TLS</div><div class="ttdoc">Level option for cy_socket_setsockopt() - TLS protocol-level option.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:68</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_ga9ce4ab8fca2ff7306f05f7433835f394"><div class="ttname"><a href="group__group__secure__sockets__macros.html#ga9ce4ab8fca2ff7306f05f7433835f394">CY_SOCKET_SO_TLS_AUTH_MODE</a></div><div class="ttdeci">#define CY_SOCKET_SO_TLS_AUTH_MODE</div><div class="ttdoc">Set the TLS authenticate mode.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:271</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_gafa0ac839774a33feeb037dbb0c449cc3"><div class="ttname"><a href="group__group__secure__sockets__macros.html#gafa0ac839774a33feeb037dbb0c449cc3">CY_SOCKET_SO_TLS_IDENTITY</a></div><div class="ttdeci">#define CY_SOCKET_SO_TLS_IDENTITY</div><div class="ttdoc">Set the TLS identity.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:242</div></div>
</div><!-- fragment --><h2><a class="anchor" id="snip3"></a>
Code Snippet 3: TCP Client Connect</h2>
<p >This code snippet demonstrates how to create a TCP client using an IPv4 address domain, and connect to a TCP server. After connection is established with the TCP server, the client sends a message to the TCP server and waits for a message from the server. </p><div class="fragment"><div class="line"> </div>
<div class="line"><span class="preprocessor">#define MAKE_IPV4_ADDRESS(a, b, c, d)     ((((uint32_t) d) &lt;&lt; 24) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  c) &lt;&lt; 16) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  b) &lt;&lt; 8) |\</span></div>
<div class="line"><span class="preprocessor">                                          ((uint32_t)   a))</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Change the server IP address and port number to match the TCP server address to which the</span></div>
<div class="line"><span class="comment"> * client wants to connect to.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"><span class="preprocessor">#define TCP_SERVER_IP_ADDRESS                     MAKE_IPV4_ADDRESS(192, 168, 18, 9)</span></div>
<div class="line"><span class="preprocessor">#define TCP_SERVER_PORT                           (50007)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Buffer size to store the incoming messages from the server. */</span></div>
<div class="line"><span class="preprocessor">#define MAX_TCP_RECV_BUFFER_SIZE                  (20)</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#define RTOS_TASK_MILLISECONDS_TO_WAIT            (1000)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Socket disconnect timeout. */</span></div>
<div class="line"><span class="preprocessor">#define SOCKET_DISCONNECT_TIMEOUT                 (0)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* TCP socket handle. */</span></div>
<div class="line"><a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> client_handle;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Flag to indicate that the message is received from the TCP server. */</span></div>
<div class="line"><span class="keywordtype">bool</span> received_msg_from_server;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Function to handle the incoming message from the TCP server. */</span></div>
<div class="line">cy_rslt_t tcp_client_recv_handler(<a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> client_handle, <span class="keywordtype">void</span> *arg)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result ;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable to store the number of bytes received. */</span></div>
<div class="line">    uint32_t bytes_received = 0;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to hold the received data from the TCP server. */</span></div>
<div class="line">    <span class="keywordtype">char</span> rx_buffer[MAX_TCP_RECV_BUFFER_SIZE];</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Receive incoming message from the TCP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gaeb1269bd36384293539dc749095412f7">cy_socket_recv</a>(client_handle, rx_buffer, MAX_TCP_RECV_BUFFER_SIZE,</div>
<div class="line">                            <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, &amp;bytes_received);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Set the flag to indicate that a message is received from the TCP server. */</span></div>
<div class="line">    received_msg_from_server = <span class="keyword">true</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> snippet_tcp_client()</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable to store the number of bytes sent to the TCP server. */</span></div>
<div class="line">    uint32_t bytes_sent = 0;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* IP address and TCP port number of the TCP server. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> tcp_server_addr = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a> = TCP_SERVER_IP_ADDRESS,</div>
<div class="line">        .ip_address.version = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a>,</div>
<div class="line">        .port = TCP_SERVER_PORT</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to hold the data to be sent to the client. */</span></div>
<div class="line">    <span class="keywordtype">char</span> tx_buffer[] = <span class="stringliteral">&quot;Message to TCP server&quot;</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a TCP socket. If you need a secure connection, create a secure socket</span></div>
<div class="line"><span class="comment">     * as demonstrated by the snippet_create_secure_socket function.</span></div>
<div class="line"><span class="comment">     */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gac0610ba6e8fe01ccbaacf89145f9285a">CY_SOCKET_TYPE_STREAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga1c6d23edbc333c85d3e7a8702dcf22a1">CY_SOCKET_IPPROTO_TCP</a>, &amp;client_handle);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable used to set the socket options. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a> tcp_recv_option = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__opt__callback__t.html#a7f321e77df24f3b06ade13f44ce43053">callback</a> = tcp_client_recv_handler,</div>
<div class="line">        .arg = NULL</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Register the callback function to handle the messages received from the TCP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(client_handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaf36b6f0f1bc53927d148746fedad7149">CY_SOCKET_SOL_SOCKET</a>,</div>
<div class="line">                                  <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaa05dc49b1682f0682d1a4066c641512d">CY_SOCKET_SO_RECEIVE_CALLBACK</a>,</div>
<div class="line">                                  &amp;tcp_recv_option, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a>));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Connect the created socket to the TCP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga2e07189d60c7e6a130302d2e2db19653">cy_socket_connect</a>(client_handle, &amp;tcp_server_addr, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a>));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Send a message to the TCP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gaf6ba24a0e4e6eb3c24087938aa3ae021">cy_socket_send</a>(client_handle, tx_buffer, strlen(tx_buffer), <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, &amp;bytes_sent);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Wait for a response message from the TCP server.*/</span></div>
<div class="line">    <span class="keywordflow">for</span>(;;)</div>
<div class="line">    {</div>
<div class="line">        <span class="keywordflow">if</span>(received_msg_from_server)</div>
<div class="line">        {</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        }</div>
<div class="line"> </div>
<div class="line">        cy_rtos_delay_milliseconds(RTOS_TASK_MILLISECONDS_TO_WAIT);</div>
<div class="line">    }</div>
<div class="line">}</div>
<div class="ttc" id="agroup__group__secure__sockets__enums_html_gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520"><div class="ttname"><a href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a></div><div class="ttdeci">@ CY_SOCKET_IP_VER_V4</div><div class="ttdoc">IPv4 protocol.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:86</div></div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_ga2e07189d60c7e6a130302d2e2db19653"><div class="ttname"><a href="group__group__secure__sockets__functions.html#ga2e07189d60c7e6a130302d2e2db19653">cy_socket_connect</a></div><div class="ttdeci">cy_rslt_t cy_socket_connect(cy_socket_t handle, cy_socket_sockaddr_t *address, uint32_t address_length)</div><div class="ttdoc">Connects a TCP/TLS socket to the specified server IP address and port.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_gaeb1269bd36384293539dc749095412f7"><div class="ttname"><a href="group__group__secure__sockets__functions.html#gaeb1269bd36384293539dc749095412f7">cy_socket_recv</a></div><div class="ttdeci">cy_rslt_t cy_socket_recv(cy_socket_t handle, void *buffer, uint32_t length, int flags, uint32_t *bytes_received)</div><div class="ttdoc">Receives the data from a connected TCP/TLS socket.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_gaf6ba24a0e4e6eb3c24087938aa3ae021"><div class="ttname"><a href="group__group__secure__sockets__functions.html#gaf6ba24a0e4e6eb3c24087938aa3ae021">cy_socket_send</a></div><div class="ttdeci">cy_rslt_t cy_socket_send(cy_socket_t handle, const void *buffer, uint32_t length, int flags, uint32_t *bytes_sent)</div><div class="ttdoc">Sends data over a connected TCP/TLS socket.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_ga91444208828cd6b96895d64b879513d4"><div class="ttname"><a href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a></div><div class="ttdeci">#define CY_SOCKET_FLAGS_NONE</div><div class="ttdoc">cy_socket_send() input flags - No flag.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:450</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_gaa05dc49b1682f0682d1a4066c641512d"><div class="ttname"><a href="group__group__secure__sockets__macros.html#gaa05dc49b1682f0682d1a4066c641512d">CY_SOCKET_SO_RECEIVE_CALLBACK</a></div><div class="ttdeci">#define CY_SOCKET_SO_RECEIVE_CALLBACK</div><div class="ttdoc">Set the callback to be called when the socket has received data.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:180</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_gaf36b6f0f1bc53927d148746fedad7149"><div class="ttname"><a href="group__group__secure__sockets__macros.html#gaf36b6f0f1bc53927d148746fedad7149">CY_SOCKET_SOL_SOCKET</a></div><div class="ttdeci">#define CY_SOCKET_SOL_SOCKET</div><div class="ttdoc">Level option for cy_socket_setsockopt() - Socket-level option.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:66</div></div>
<div class="ttc" id="astructcy__socket__ip__address__t_html_a1b68b61521bd0b095ed35bea52f6fd82"><div class="ttname"><a href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">cy_socket_ip_address_t::v4</a></div><div class="ttdeci">uint32_t v4</div><div class="ttdoc">IPv4 address in network byte order.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:148</div></div>
<div class="ttc" id="astructcy__socket__ip__address__t_html_a98f8abed868445e1a8348da559d4f574"><div class="ttname"><a href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">cy_socket_ip_address_t::ip</a></div><div class="ttdeci">union cy_socket_ip_address_t::@0 ip</div><div class="ttdoc">IP address bytes.</div></div>
<div class="ttc" id="astructcy__socket__opt__callback__t_html"><div class="ttname"><a href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a></div><div class="ttdoc">Option value type for CY_SOCKET_SO_CONNECT_REQUEST_CALLBACK, CY_SOCKET_SO_RECEIVE_CALLBACK,...</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:167</div></div>
<div class="ttc" id="astructcy__socket__opt__callback__t_html_a7f321e77df24f3b06ade13f44ce43053"><div class="ttname"><a href="structcy__socket__opt__callback__t.html#a7f321e77df24f3b06ade13f44ce43053">cy_socket_opt_callback_t::callback</a></div><div class="ttdeci">cy_socket_callback_t callback</div><div class="ttdoc">Pointer to a caller-defined callback function.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:168</div></div>
<div class="ttc" id="astructcy__socket__sockaddr__t_html"><div class="ttname"><a href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a></div><div class="ttdoc">Socket Address.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:157</div></div>
<div class="ttc" id="astructcy__socket__sockaddr__t_html_ac4caf040376b30c85ca9065a20d5616c"><div class="ttname"><a href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">cy_socket_sockaddr_t::ip_address</a></div><div class="ttdeci">cy_socket_ip_address_t ip_address</div><div class="ttdoc">IP Address.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:159</div></div>
</div><!-- fragment --><h2><a class="anchor" id="snip4"></a>
Code Snippet 4: TCP Server - Listening for client connection</h2>
<p >This code snippet demonstrates how to create a TCP server using an IPv4 address domain and accept a TCP client connection. The server implementation in this snippet waits for a client connection and a message from the connected client. Upon receiving a message from the client, it sends a response message back to the client. </p><div class="fragment"><div class="line"><span class="preprocessor">#define MAKE_IPV4_ADDRESS(a, b, c, d)     ((((uint32_t) d) &lt;&lt; 24) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  c) &lt;&lt; 16) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  b) &lt;&lt; 8) |\</span></div>
<div class="line"><span class="preprocessor">                                          ((uint32_t)   a))</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Change the server IP address and port to match the TCP server address.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"><span class="preprocessor">#define TCP_SERVER_IP_ADDRESS                     MAKE_IPV4_ADDRESS(192, 168, 18, 9)</span></div>
<div class="line"><span class="preprocessor">#define TCP_SERVER_PORT                           (50007)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Maximum number of incoming connections. */</span></div>
<div class="line"><span class="preprocessor">#define TCP_SERVER_MAX_PENDING_CONNECTIONS        (3)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Buffer size to store the incoming messages from the client. */</span></div>
<div class="line"><span class="preprocessor">#define MAX_TCP_RECV_BUFFER_SIZE                  (20)</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#define RTOS_TASK_MILLISECONDS_TO_WAIT            (1000)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Socket disconnect timeout. */</span></div>
<div class="line"><span class="preprocessor">#define SOCKET_DISCONNECT_TIMEOUT                 (0)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* TCP socket handles. */</span></div>
<div class="line"><a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> server_handle, client_handle;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Flag to indicate when to send the data to the TCP client. */</span></div>
<div class="line"><span class="keywordtype">bool</span> send_msg_to_client;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Incoming TCP client connection handler. */</span></div>
<div class="line">cy_rslt_t tcp_connection_handler(<a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> socket_handle, <span class="keywordtype">void</span> *arg)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Client socket address. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> client_addr;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Size of the client socket address. */</span></div>
<div class="line">    uint32_t client_addr_len;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Accept a new incoming connection from a TCP client.*/</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49ede56f180b2bb3e367924f535bb300">cy_socket_accept</a>(socket_handle, &amp;client_addr, &amp;client_addr_len,</div>
<div class="line">                              &amp;client_handle);</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Function to handle the incoming  message from the TCP client. */</span></div>
<div class="line">cy_rslt_t tcp_server_recv_handler(<a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> socket_handle, <span class="keywordtype">void</span> *arg)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result ;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable to store the number of bytes received. */</span></div>
<div class="line">    uint32_t bytes_received = 0;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to hold the received data from the TCP client. */</span></div>
<div class="line">    <span class="keywordtype">char</span> rx_buffer[MAX_TCP_RECV_BUFFER_SIZE];</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Receive the incoming message from the TCP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gaeb1269bd36384293539dc749095412f7">cy_socket_recv</a>(socket_handle, rx_buffer, MAX_TCP_RECV_BUFFER_SIZE,</div>
<div class="line">                            <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, &amp;bytes_received);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Set the flag to send message to the TCP client.*/</span></div>
<div class="line">    send_msg_to_client = <span class="keyword">true</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> snippet_tcp_server()</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable to store the number of bytes sent to the TCP client. */</span></div>
<div class="line">    uint32_t bytes_sent = 0;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* IP address and TCP port number of the TCP server. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> tcp_server_addr = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a> = TCP_SERVER_IP_ADDRESS,</div>
<div class="line">        .ip_address.version = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a>,</div>
<div class="line">        .port = TCP_SERVER_PORT</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to hold the data to be sent to the client. */</span></div>
<div class="line">    <span class="keywordtype">char</span> tx_buffer[] = <span class="stringliteral">&quot;Message to TCP client&quot;</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a TCP socket. If you need a secure connection, create a secure socket</span></div>
<div class="line"><span class="comment">     * as demonstrated by the snippet_create_secure_socket function.</span></div>
<div class="line"><span class="comment">     */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gac0610ba6e8fe01ccbaacf89145f9285a">CY_SOCKET_TYPE_STREAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga1c6d23edbc333c85d3e7a8702dcf22a1">CY_SOCKET_IPPROTO_TCP</a>, &amp;server_handle);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable used to set the socket options. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a> tcp_connection_option = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__opt__callback__t.html#a7f321e77df24f3b06ade13f44ce43053">callback</a> = tcp_connection_handler,</div>
<div class="line">        .arg = NULL</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Register the callback function to handle the connection request from a TCP client. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(server_handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaf36b6f0f1bc53927d148746fedad7149">CY_SOCKET_SOL_SOCKET</a>,</div>
<div class="line">                                <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga7fd0bff2f890ddab81f7847a1862c325">CY_SOCKET_SO_CONNECT_REQUEST_CALLBACK</a>,</div>
<div class="line">                                &amp;tcp_connection_option, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a>));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable used to set the socket options. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a> tcp_recv_option = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__opt__callback__t.html#a7f321e77df24f3b06ade13f44ce43053">callback</a> = tcp_server_recv_handler,</div>
<div class="line">        .arg = NULL</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Register the callback function to handle the messages received from the TCP client. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(server_handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaf36b6f0f1bc53927d148746fedad7149">CY_SOCKET_SOL_SOCKET</a>,</div>
<div class="line">                                  <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaa05dc49b1682f0682d1a4066c641512d">CY_SOCKET_SO_RECEIVE_CALLBACK</a>,</div>
<div class="line">                                  &amp;tcp_recv_option, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a>));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Bind the TCP socket created to the server IP address and to the TCP port. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gac05772b8936d96ec8d8367b661912608">cy_socket_bind</a>(server_handle, &amp;tcp_server_addr, <span class="keyword">sizeof</span>(tcp_server_addr));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Start listening on the TCP server socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga1d5c6e81830f6c81f820e02f6d0cf6a0">cy_socket_listen</a>(server_handle, TCP_SERVER_MAX_PENDING_CONNECTIONS);</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">for</span> (;;)</div>
<div class="line">    {</div>
<div class="line">        <span class="keywordflow">if</span>(send_msg_to_client)</div>
<div class="line">        {</div>
<div class="line">            <span class="comment">/* Send a message to the TCP client. */</span></div>
<div class="line">            result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gaf6ba24a0e4e6eb3c24087938aa3ae021">cy_socket_send</a>(client_handle, tx_buffer, strlen(tx_buffer),</div>
<div class="line">                                    <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, &amp;bytes_sent);</div>
<div class="line"> </div>
<div class="line">            send_msg_to_client = <span class="keyword">false</span>;</div>
<div class="line">        }</div>
<div class="line"> </div>
<div class="line">        cy_rtos_delay_milliseconds(RTOS_TASK_MILLISECONDS_TO_WAIT);</div>
<div class="line">    }</div>
<div class="line">}</div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_ga1d5c6e81830f6c81f820e02f6d0cf6a0"><div class="ttname"><a href="group__group__secure__sockets__functions.html#ga1d5c6e81830f6c81f820e02f6d0cf6a0">cy_socket_listen</a></div><div class="ttdeci">cy_rslt_t cy_socket_listen(cy_socket_t handle, int backlog)</div><div class="ttdoc">Listens for TCP/TLS socket connections and limits the queue of incoming connections.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_ga49ede56f180b2bb3e367924f535bb300"><div class="ttname"><a href="group__group__secure__sockets__functions.html#ga49ede56f180b2bb3e367924f535bb300">cy_socket_accept</a></div><div class="ttdeci">cy_rslt_t cy_socket_accept(cy_socket_t handle, cy_socket_sockaddr_t *address, uint32_t *address_length, cy_socket_t *socket)</div><div class="ttdoc">Accepts a new TCP/TLS connection on a socket.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_gac05772b8936d96ec8d8367b661912608"><div class="ttname"><a href="group__group__secure__sockets__functions.html#gac05772b8936d96ec8d8367b661912608">cy_socket_bind</a></div><div class="ttdeci">cy_rslt_t cy_socket_bind(cy_socket_t handle, cy_socket_sockaddr_t *address, uint32_t address_length)</div><div class="ttdoc">Binds the socket to the given socket address.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_ga7fd0bff2f890ddab81f7847a1862c325"><div class="ttname"><a href="group__group__secure__sockets__macros.html#ga7fd0bff2f890ddab81f7847a1862c325">CY_SOCKET_SO_CONNECT_REQUEST_CALLBACK</a></div><div class="ttdeci">#define CY_SOCKET_SO_CONNECT_REQUEST_CALLBACK</div><div class="ttdoc">Set the callback to be called upon an incoming client connection request.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:163</div></div>
</div><!-- fragment --><h2><a class="anchor" id="snip5"></a>
Code Snippet 5: TCP IPv6 Client Connect</h2>
<p >This code snippet demonstrates how to create a TCP client using an IPv6 address domain and connect to a TCP server. After connection is established with the TCP server, the client sends a message to the TCP server and waits for a message from the server. </p><div class="fragment"><div class="line"> </div>
<div class="line"><span class="comment">/* Converts a 16-bit value from host byte order (little-endian) to network byte order (big-endian) */</span></div>
<div class="line"><span class="preprocessor">#define HTONS(x) ( ( ( (x) &amp; 0x0000FF00) &gt;&gt; 8 ) | ((x) &amp; 0x000000FF) &lt;&lt; 8 )</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Arguments passed are uint16_t and in little-endian format. */</span></div>
<div class="line"><span class="preprocessor">#define MAKE_IPV6_ADDRESS(a, b, c, d, e, f, g, h) { \</span></div>
<div class="line"><span class="preprocessor">                                                    ( (uint32_t) (HTONS(a)) | ( (uint32_t) (HTONS(b)) &lt;&lt; 16 ) ), \</span></div>
<div class="line"><span class="preprocessor">                                                    ( (uint32_t) (HTONS(c)) | ( (uint32_t) (HTONS(d)) &lt;&lt; 16 ) ), \</span></div>
<div class="line"><span class="preprocessor">                                                    ( (uint32_t) (HTONS(e)) | ( (uint32_t) (HTONS(f)) &lt;&lt; 16 ) ), \</span></div>
<div class="line"><span class="preprocessor">                                                    ( (uint32_t) (HTONS(g)) | ( (uint32_t) (HTONS(h)) &lt;&lt; 16 ) ), \</span></div>
<div class="line"><span class="preprocessor">                                                  }</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Change the server IPv6 address and port number to match the TCP server address to which the client wants to connect to. */</span></div>
<div class="line"><span class="preprocessor">#define TCP_SERVER_IPV6_ADDRESS                   MAKE_IPV6_ADDRESS( 0xFE80, 0, 0, 0, 0x29D, 0x6BFF, 0xFE9D, 0xE2B4)</span></div>
<div class="line"><span class="preprocessor">#define TCP_SERVER_PORT                           (50007)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Buffer size to store the incoming messages from the server. */</span></div>
<div class="line"><span class="preprocessor">#define MAX_TCP_RECV_BUFFER_SIZE                  (20)</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#define RTOS_TASK_MILLISECONDS_TO_WAIT            (1000)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Socket disconnect timeout. */</span></div>
<div class="line"><span class="preprocessor">#define SOCKET_DISCONNECT_TIMEOUT                 (0)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* TCP socket handle. */</span></div>
<div class="line"><a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> ipv6_client_handle;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Flag to indicate that message is received from the TCP server. */</span></div>
<div class="line"><span class="keywordtype">bool</span> received_msg_from_server;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Function to handle the incoming message from the TCP server. */</span></div>
<div class="line">cy_rslt_t tcp_ipv6_client_recv_handler(<a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> client_handle, <span class="keywordtype">void</span> *arg)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result ;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable to store the number of bytes received. */</span></div>
<div class="line">    uint32_t bytes_received = 0;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to store the incoming data from the TCP server. */</span></div>
<div class="line">    <span class="keywordtype">char</span> rx_buffer[MAX_TCP_RECV_BUFFER_SIZE];</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Receive the incoming message from the TCP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gaeb1269bd36384293539dc749095412f7">cy_socket_recv</a>(client_handle, rx_buffer, MAX_TCP_RECV_BUFFER_SIZE,</div>
<div class="line">                            <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, &amp;bytes_received);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Set the flag to indicate that a message is received from the TCP server. */</span></div>
<div class="line">    received_msg_from_server = <span class="keyword">true</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> snippet_tcp_ipv6_client()</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable to store the number of bytes sent to the TCP server. */</span></div>
<div class="line">    uint32_t bytes_sent = 0;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* IP address and TCP port number of the TCP server. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> tcp_server_addr = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#af849e54b89b4412aaca8eb46275f2b4b">v6</a> = TCP_SERVER_IPV6_ADDRESS,</div>
<div class="line">        .ip_address.version = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789daa5efbdd596c434140bed0f8f0d5d9582">CY_SOCKET_IP_VER_V6</a>,</div>
<div class="line">        .port = TCP_SERVER_PORT</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to hold the data to be sent to the client. */</span></div>
<div class="line">    <span class="keywordtype">char</span> tx_buffer[] = <span class="stringliteral">&quot;Message to TCP server&quot;</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a TCP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gaa2d04e27350109a0084d8ea90eb24a27">CY_SOCKET_DOMAIN_AF_INET6</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gac0610ba6e8fe01ccbaacf89145f9285a">CY_SOCKET_TYPE_STREAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga1c6d23edbc333c85d3e7a8702dcf22a1">CY_SOCKET_IPPROTO_TCP</a>, &amp;client_handle);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable used to set the socket options. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a> tcp_recv_option = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__opt__callback__t.html#a7f321e77df24f3b06ade13f44ce43053">callback</a> = tcp_ipv6_client_recv_handler,</div>
<div class="line">        .arg = NULL</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Register the callback function to handle the messages received from the TCP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(client_handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaf36b6f0f1bc53927d148746fedad7149">CY_SOCKET_SOL_SOCKET</a>,</div>
<div class="line">                                  <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaa05dc49b1682f0682d1a4066c641512d">CY_SOCKET_SO_RECEIVE_CALLBACK</a>,</div>
<div class="line">                                  &amp;tcp_recv_option, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a>));</div>
<div class="line"> </div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga2e07189d60c7e6a130302d2e2db19653">cy_socket_connect</a>(client_handle, &amp;tcp_server_addr, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a>));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Send a message to the TCP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gaf6ba24a0e4e6eb3c24087938aa3ae021">cy_socket_send</a>(client_handle, tx_buffer, strlen(tx_buffer), <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, &amp;bytes_sent);</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">for</span>(;;)</div>
<div class="line">    {</div>
<div class="line">        <span class="keywordflow">if</span>(received_msg_from_server)</div>
<div class="line">        {</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        }</div>
<div class="line"> </div>
<div class="line">        cy_rtos_delay_milliseconds(RTOS_TASK_MILLISECONDS_TO_WAIT);</div>
<div class="line">    }</div>
<div class="line">}</div>
<div class="ttc" id="agroup__group__secure__sockets__enums_html_gga97cb501a58195859467b126cf2a2789daa5efbdd596c434140bed0f8f0d5d9582"><div class="ttname"><a href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789daa5efbdd596c434140bed0f8f0d5d9582">CY_SOCKET_IP_VER_V6</a></div><div class="ttdeci">@ CY_SOCKET_IP_VER_V6</div><div class="ttdoc">IPv6 protocol.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:87</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_gaa2d04e27350109a0084d8ea90eb24a27"><div class="ttname"><a href="group__group__secure__sockets__macros.html#gaa2d04e27350109a0084d8ea90eb24a27">CY_SOCKET_DOMAIN_AF_INET6</a></div><div class="ttdeci">#define CY_SOCKET_DOMAIN_AF_INET6</div><div class="ttdoc">Domain option for cy_socket_create() - IPv6 internet protocols.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:48</div></div>
<div class="ttc" id="astructcy__socket__ip__address__t_html_af849e54b89b4412aaca8eb46275f2b4b"><div class="ttname"><a href="structcy__socket__ip__address__t.html#af849e54b89b4412aaca8eb46275f2b4b">cy_socket_ip_address_t::v6</a></div><div class="ttdeci">uint32_t v6[4]</div><div class="ttdoc">IPv6 address in network byte order.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:149</div></div>
</div><!-- fragment --><h2><a class="anchor" id="snip6"></a>
Code Snippet 6: Create UDP Socket</h2>
<p >This code snippet demonstrates how to initialize and create a UDP socket using an IPv4 address domain. </p><div class="fragment"><div class="line"><span class="keywordtype">void</span> snippet_create_udp_socket()</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* UDP socket handle. */</span></div>
<div class="line">    <a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> socket_handle;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a UDP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga9ea5c6f97612e469393306ddefc38657">CY_SOCKET_TYPE_DGRAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga2bbff715a00d6ce5ff669b12a75c7441">CY_SOCKET_IPPROTO_UDP</a>, &amp;socket_handle);</div>
<div class="line">}</div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_ga2bbff715a00d6ce5ff669b12a75c7441"><div class="ttname"><a href="group__group__secure__sockets__macros.html#ga2bbff715a00d6ce5ff669b12a75c7441">CY_SOCKET_IPPROTO_UDP</a></div><div class="ttdeci">#define CY_SOCKET_IPPROTO_UDP</div><div class="ttdoc">Protocol option for cy_socket_create() - UDP.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:60</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_ga9ea5c6f97612e469393306ddefc38657"><div class="ttname"><a href="group__group__secure__sockets__macros.html#ga9ea5c6f97612e469393306ddefc38657">CY_SOCKET_TYPE_DGRAM</a></div><div class="ttdeci">#define CY_SOCKET_TYPE_DGRAM</div><div class="ttdoc">Type parameter for cy_socket_create() - Datagram.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:53</div></div>
</div><!-- fragment --><h2><a class="anchor" id="snip7"></a>
Code Snippet 7: UDP Client - Send data to a UDP server using an IPv4 address</h2>
<p >This code snippet demonstrates how to create a UDP client using an IPv4 address domain and send data to a UDP server. After sending data to the UDP server, this UDP client waits for a response message from the server. </p><div class="fragment"><div class="line"> </div>
<div class="line"><span class="preprocessor">#define MAKE_IPV4_ADDRESS(a, b, c, d)     ((((uint32_t) d) &lt;&lt; 24) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  c) &lt;&lt; 16) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  b) &lt;&lt; 8) |\</span></div>
<div class="line"><span class="preprocessor">                                          ((uint32_t)   a))</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Change the server IP address and port number to match the UDP server address to which</span></div>
<div class="line"><span class="comment"> * client wants to connect to.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"><span class="preprocessor">#define UDP_SERVER_IP_ADDRESS                     MAKE_IPV4_ADDRESS(192, 168, 18, 9)</span></div>
<div class="line"><span class="preprocessor">#define UDP_SERVER_PORT                           (50007)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Buffer size to store the incoming messages from the server. */</span></div>
<div class="line"><span class="preprocessor">#define MAX_UDP_RECV_BUFFER_SIZE                  (20)</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#define RTOS_TASK_MILLISECONDS_TO_WAIT            (1000)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* UDP socket handle. */</span></div>
<div class="line"><a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> client_handle;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">bool</span> received_msg_from_udp_server;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Function to handle the incoming  message from the UDP server. */</span></div>
<div class="line">cy_rslt_t udp_client_recv_handler(<a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> socket_handle, <span class="keywordtype">void</span> *arg)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result ;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable to store the number of bytes received. */</span></div>
<div class="line">    uint32_t bytes_received = 0;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to store the incoming data from the UDP server. */</span></div>
<div class="line">    <span class="keywordtype">char</span> rx_buffer[MAX_UDP_RECV_BUFFER_SIZE];</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Receive the incoming message from the UDP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga29df5cbdaa9de0f324cf947454577727">cy_socket_recvfrom</a>(socket_handle, rx_buffer, MAX_UDP_RECV_BUFFER_SIZE, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, NULL, NULL, &amp;bytes_received);</div>
<div class="line"> </div>
<div class="line">    received_msg_from_udp_server = <span class="keyword">true</span>;</div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> snippet_udp_client()</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable to store the number of bytes sent to the UDP server. */</span></div>
<div class="line">    uint32_t bytes_sent = 0;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* IP address and UDP port number of the UDP server. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> udp_server_addr = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a> = UDP_SERVER_IP_ADDRESS,</div>
<div class="line">        .ip_address.version = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a>,</div>
<div class="line">        .port = UDP_SERVER_PORT</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to hold the data to be sent to client. */</span></div>
<div class="line">    <span class="keywordtype">char</span> tx_buffer[] = <span class="stringliteral">&quot;Message to UDP server&quot;</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a UDP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga9ea5c6f97612e469393306ddefc38657">CY_SOCKET_TYPE_DGRAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga2bbff715a00d6ce5ff669b12a75c7441">CY_SOCKET_IPPROTO_UDP</a>, &amp;client_handle);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable used to set the socket options. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a> udp_recv_option = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__opt__callback__t.html#a7f321e77df24f3b06ade13f44ce43053">callback</a> = udp_client_recv_handler,</div>
<div class="line">        .arg = NULL</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Register the callback function to handle the messages received from the UDP client. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(client_handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaf36b6f0f1bc53927d148746fedad7149">CY_SOCKET_SOL_SOCKET</a>,</div>
<div class="line">                                  <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaa05dc49b1682f0682d1a4066c641512d">CY_SOCKET_SO_RECEIVE_CALLBACK</a>,</div>
<div class="line">                                  &amp;udp_recv_option, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a>));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Send a message to the UDP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga4bc4e8aed9916206f073cb22cf4009cc">cy_socket_sendto</a>(client_handle, tx_buffer, strlen(tx_buffer), <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, &amp;udp_server_addr, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a>), &amp;bytes_sent);</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">for</span>(;;)</div>
<div class="line">    {</div>
<div class="line">        <span class="keywordflow">if</span>(received_msg_from_udp_server)</div>
<div class="line">        {</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        }</div>
<div class="line"> </div>
<div class="line">        cy_rtos_delay_milliseconds(RTOS_TASK_MILLISECONDS_TO_WAIT);</div>
<div class="line">    }</div>
<div class="line">}</div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_ga29df5cbdaa9de0f324cf947454577727"><div class="ttname"><a href="group__group__secure__sockets__functions.html#ga29df5cbdaa9de0f324cf947454577727">cy_socket_recvfrom</a></div><div class="ttdeci">cy_rslt_t cy_socket_recvfrom(cy_socket_t handle, void *buffer, uint32_t length, int flags, cy_socket_sockaddr_t *src_addr, uint32_t *src_addr_length, uint32_t *bytes_received)</div><div class="ttdoc">Receives a UDP datagram for the specified socket.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_ga4bc4e8aed9916206f073cb22cf4009cc"><div class="ttname"><a href="group__group__secure__sockets__functions.html#ga4bc4e8aed9916206f073cb22cf4009cc">cy_socket_sendto</a></div><div class="ttdeci">cy_rslt_t cy_socket_sendto(cy_socket_t handle, const void *buffer, uint32_t length, int flags, const cy_socket_sockaddr_t *dest_addr, uint32_t address_length, uint32_t *bytes_sent)</div><div class="ttdoc">Sends a UDP datagram over a specified socket.</div></div>
</div><!-- fragment --><h2><a class="anchor" id="snip8"></a>
Code Snippet 8: UDP Server - Receive data from a UDP client using an IPv4 address</h2>
<p >This code snippet demonstrates how to create a UDP server using an IPv4 address domain and receive data from a UDP client. </p><div class="fragment"><div class="line"> </div>
<div class="line"><span class="preprocessor">#define MAKE_IPV4_ADDRESS(a, b, c, d)     ((((uint32_t) d) &lt;&lt; 24) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  c) &lt;&lt; 16) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  b) &lt;&lt; 8) |\</span></div>
<div class="line"><span class="preprocessor">                                          ((uint32_t)   a))</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Change the server IP address and port number to match the UDP server address to which</span></div>
<div class="line"><span class="comment"> * client wants to connect to.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"><span class="preprocessor">#define UDP_SERVER_IP_ADDRESS                     MAKE_IPV4_ADDRESS(192, 168, 18, 9)</span></div>
<div class="line"><span class="preprocessor">#define UDP_SERVER_PORT                           (50007)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Buffer size to store the incoming messages from the client. */</span></div>
<div class="line"><span class="preprocessor">#define MAX_UDP_RECV_BUFFER_SIZE                  (20)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* UDP socket handle. */</span></div>
<div class="line"><a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> server_handle;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">bool</span> received_msg_from_udp_client;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Function to handle the incoming  message from the UDP client. */</span></div>
<div class="line">cy_rslt_t udp_server_recv_handler(<a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> socket_handle, <span class="keywordtype">void</span> *arg)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result ;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable to store the number of bytes received. */</span></div>
<div class="line">    uint32_t bytes_received = 0;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordtype">char</span> rx_buffer[MAX_UDP_RECV_BUFFER_SIZE];</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Receive the incoming message from the UDP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga29df5cbdaa9de0f324cf947454577727">cy_socket_recvfrom</a>(server_handle, rx_buffer, MAX_UDP_RECV_BUFFER_SIZE, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, NULL, NULL, &amp;bytes_received);</div>
<div class="line"> </div>
<div class="line">    received_msg_from_udp_client = <span class="keyword">true</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> snippet_udp_server()</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* IP address and UDP port number of the UDP server. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> udp_server_addr = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a> = UDP_SERVER_IP_ADDRESS,</div>
<div class="line">        .ip_address.version = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a>,</div>
<div class="line">        .port = UDP_SERVER_PORT</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a UDP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga9ea5c6f97612e469393306ddefc38657">CY_SOCKET_TYPE_DGRAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga2bbff715a00d6ce5ff669b12a75c7441">CY_SOCKET_IPPROTO_UDP</a>, &amp;server_handle);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Variable used to set the socket options. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a> udp_recv_option = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__opt__callback__t.html#a7f321e77df24f3b06ade13f44ce43053">callback</a> = udp_server_recv_handler,</div>
<div class="line">        .arg = NULL</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Register the callback function to handle the messages received from the UDP client. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(server_handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaf36b6f0f1bc53927d148746fedad7149">CY_SOCKET_SOL_SOCKET</a>,</div>
<div class="line">                                  <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaa05dc49b1682f0682d1a4066c641512d">CY_SOCKET_SO_RECEIVE_CALLBACK</a>,</div>
<div class="line">                                  &amp;udp_recv_option, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__opt__callback__t.html">cy_socket_opt_callback_t</a>));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Bind the UDP socket created to the server IP address and port. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gac05772b8936d96ec8d8367b661912608">cy_socket_bind</a>(server_handle, &amp;udp_server_addr, <span class="keyword">sizeof</span>(udp_server_addr));</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">for</span>(;;)</div>
<div class="line">    {</div>
<div class="line">        <span class="keywordflow">if</span>(received_msg_from_udp_client)</div>
<div class="line">        {</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        }</div>
<div class="line"> </div>
<div class="line">        cy_rtos_delay_milliseconds(RTOS_TASK_MILLISECONDS_TO_WAIT);</div>
<div class="line">    }</div>
<div class="line">}</div>
</div><!-- fragment --><h2><a class="anchor" id="snip9"></a>
Code Snippet 9: IPv4 multicast group - Join and leave an IPv4 multicast group</h2>
<p >This code snippet demonstrates how to join an IPv4 multicast group, send a packet to the IPv4 multicast address, receive a packet from the IPv4 multicast group, and leave the IPv4 multicast group. </p><div class="fragment"><div class="line"> </div>
<div class="line"><span class="preprocessor">#define MAKE_IPV4_ADDRESS(a, b, c, d)     ((((uint32_t) d) &lt;&lt; 24) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  c) &lt;&lt; 16) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  b) &lt;&lt; 8) |\</span></div>
<div class="line"><span class="preprocessor">                                          ((uint32_t)   a))</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* IPv4 multicast group address. Change it to the mutlicast group address you want to join. */</span></div>
<div class="line"><span class="preprocessor">#define IPV4_MULTICAST_GROUP_ADDRESS      MAKE_IPV4_ADDRESS(224, 1, 1, 1)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* UDP Port number. Change it to the required port number. */</span></div>
<div class="line"><span class="preprocessor">#define UDP_PORT                          (50007)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* IPv4 address of your device. Change it to the IPv4 address of your device. */</span></div>
<div class="line"><span class="preprocessor">#define IPV4_DEVICE_ADDRESS               MAKE_IPV4_ADDRESS(192, 168, 18, 9)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Buffer size to store the incoming multicast message. */</span></div>
<div class="line"><span class="preprocessor">#define MAX_UDP_RECV_BUFFER_SIZE          (20)</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> snippet_udp_ipv4_multicast_join_leave()</div>
<div class="line">{</div>
<div class="line">    <a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> handle;</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    uint32_t bytes_sent = 0;</div>
<div class="line">    uint32_t bytes_received = 0;</div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__ip__mreq__t.html">cy_socket_ip_mreq_t</a> imr;</div>
<div class="line"> </div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to hold the data to be sent to the multicast group. */</span></div>
<div class="line">    <span class="keywordtype">char</span> tx_buffer[] = <span class="stringliteral">&quot;Message to multicast group&quot;</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to store the incoming message from the multicast group. */</span></div>
<div class="line">    <span class="keywordtype">char</span> rx_buffer[MAX_UDP_RECV_BUFFER_SIZE];</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/*</span></div>
<div class="line"><span class="comment">     * Add the code to obtain the IPv4 address of the device.</span></div>
<div class="line"><span class="comment">     */</span></div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* IPv4 address of the device. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__ip__address__t.html">cy_socket_ip_address_t</a> ip_addr = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a> = IPV4_DEVICE_ADDRESS, <span class="comment">/* Attention: This IP address is hardcoded with random address in this snippet. It should be updated with the actual device IPv4 address. */</span></div>
<div class="line">        .version = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a></div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* IPv4 address of the multicast group. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__ip__address__t.html">cy_socket_ip_address_t</a> multicast_address = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a> = IPV4_MULTICAST_GROUP_ADDRESS,</div>
<div class="line">        .version = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a></div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Socket address of the multicast group. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> socket_address = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a> = multicast_address,</div>
<div class="line">        .port = UDP_PORT</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Fill the multicast request variable. */</span></div>
<div class="line">    imr.<a class="code hl_variable" href="structcy__socket__ip__mreq__t.html#af355ca415d98f381b1e39d95a3741acc">if_addr</a> = ip_addr;</div>
<div class="line">    imr.<a class="code hl_variable" href="structcy__socket__ip__mreq__t.html#afbd0341c76b9026737ee7db33237fb8e">multi_addr</a> = multicast_address;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a UDP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga9ea5c6f97612e469393306ddefc38657">CY_SOCKET_TYPE_DGRAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga2bbff715a00d6ce5ff669b12a75c7441">CY_SOCKET_IPPROTO_UDP</a>, &amp;handle);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Join an IPv4 multicast group. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga6158e1e598a55be5e42d5db5f921d13e">CY_SOCKET_SOL_IP</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga6eb3fccf457611f775c37e58442449b3">CY_SOCKET_SO_JOIN_MULTICAST_GROUP</a>, &amp;imr, <span class="keyword">sizeof</span>(imr));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Bind the UDP socket created to the multicast address and port. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gac05772b8936d96ec8d8367b661912608">cy_socket_bind</a>(handle, &amp;socket_address, <span class="keyword">sizeof</span>(socket_address));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Send a message to the multicast group. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga4bc4e8aed9916206f073cb22cf4009cc">cy_socket_sendto</a>(handle, tx_buffer, strlen(tx_buffer), <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, &amp;socket_address, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a>), &amp;bytes_sent);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Receive a message from the multicast group. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga29df5cbdaa9de0f324cf947454577727">cy_socket_recvfrom</a>(handle, rx_buffer, MAX_UDP_RECV_BUFFER_SIZE, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, NULL, NULL, &amp;bytes_received);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Leave the multicast group. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga6158e1e598a55be5e42d5db5f921d13e">CY_SOCKET_SOL_IP</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaa3b9e9b88b1affecbb653fb1375771e3">CY_SOCKET_SO_LEAVE_MULTICAST_GROUP</a>, &amp;imr, <span class="keyword">sizeof</span>(imr));</div>
<div class="line">}</div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_ga6158e1e598a55be5e42d5db5f921d13e"><div class="ttname"><a href="group__group__secure__sockets__macros.html#ga6158e1e598a55be5e42d5db5f921d13e">CY_SOCKET_SOL_IP</a></div><div class="ttdeci">#define CY_SOCKET_SOL_IP</div><div class="ttdoc">Level option for cy_socket_setsockopt() - IP protocol-level option.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:69</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_ga6eb3fccf457611f775c37e58442449b3"><div class="ttname"><a href="group__group__secure__sockets__macros.html#ga6eb3fccf457611f775c37e58442449b3">CY_SOCKET_SO_JOIN_MULTICAST_GROUP</a></div><div class="ttdeci">#define CY_SOCKET_SO_JOIN_MULTICAST_GROUP</div><div class="ttdoc">Join an IPv4 (or) IPv6 multicast group.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:296</div></div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_gaa3b9e9b88b1affecbb653fb1375771e3"><div class="ttname"><a href="group__group__secure__sockets__macros.html#gaa3b9e9b88b1affecbb653fb1375771e3">CY_SOCKET_SO_LEAVE_MULTICAST_GROUP</a></div><div class="ttdeci">#define CY_SOCKET_SO_LEAVE_MULTICAST_GROUP</div><div class="ttdoc">Leave an IPv4 (or) IPv6 multicast group.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:306</div></div>
<div class="ttc" id="astructcy__socket__ip__address__t_html"><div class="ttname"><a href="structcy__socket__ip__address__t.html">cy_socket_ip_address_t</a></div><div class="ttdoc">IP Address Structure.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:143</div></div>
<div class="ttc" id="astructcy__socket__ip__mreq__t_html"><div class="ttname"><a href="structcy__socket__ip__mreq__t.html">cy_socket_ip_mreq_t</a></div><div class="ttdoc">Option value type for CY_SOCKET_SO_JOIN_MULTICAST_GROUP, and CY_SOCKET_SO_LEAVE_MULTICAST_GROUP socke...</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:175</div></div>
<div class="ttc" id="astructcy__socket__ip__mreq__t_html_af355ca415d98f381b1e39d95a3741acc"><div class="ttname"><a href="structcy__socket__ip__mreq__t.html#af355ca415d98f381b1e39d95a3741acc">cy_socket_ip_mreq_t::if_addr</a></div><div class="ttdeci">cy_socket_ip_address_t if_addr</div><div class="ttdoc">Local IP address of the interface.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:177</div></div>
<div class="ttc" id="astructcy__socket__ip__mreq__t_html_afbd0341c76b9026737ee7db33237fb8e"><div class="ttname"><a href="structcy__socket__ip__mreq__t.html#afbd0341c76b9026737ee7db33237fb8e">cy_socket_ip_mreq_t::multi_addr</a></div><div class="ttdeci">cy_socket_ip_address_t multi_addr</div><div class="ttdoc">IP multicast address of the group.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:176</div></div>
</div><!-- fragment --><h2><a class="anchor" id="snip10"></a>
Code Snippet 10: IPv6 multicast group - Join and leave an IPv6 multicast group</h2>
<p >This code snippet demonstrates how to join an IPv6 multicast group, send a packet to the IPv6 multicast address, receive a packet from the IPv6 multicast group, and leave the IPv6 multicast group. </p><div class="fragment"><div class="line"> </div>
<div class="line"><span class="comment">/* Converts a 16-bit value from host byte order (little-endian) to network byte order (big-endian) */</span></div>
<div class="line"><span class="preprocessor">#define HTONS(x) ( ( ( (x) &amp; 0x0000FF00) &gt;&gt; 8 ) | ((x) &amp; 0x000000FF) &lt;&lt; 8 )</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Arguments passed are uint16_t and in little-endian format. */</span></div>
<div class="line"><span class="preprocessor">#define MAKE_IPV6_ADDRESS(a, b, c, d, e, f, g, h) { \</span></div>
<div class="line"><span class="preprocessor">                                                    ( (uint32_t) (HTONS(a)) | ( (uint32_t) (HTONS(b)) &lt;&lt; 16 ) ), \</span></div>
<div class="line"><span class="preprocessor">                                                    ( (uint32_t) (HTONS(c)) | ( (uint32_t) (HTONS(d)) &lt;&lt; 16 ) ), \</span></div>
<div class="line"><span class="preprocessor">                                                    ( (uint32_t) (HTONS(e)) | ( (uint32_t) (HTONS(f)) &lt;&lt; 16 ) ), \</span></div>
<div class="line"><span class="preprocessor">                                                    ( (uint32_t) (HTONS(g)) | ( (uint32_t) (HTONS(h)) &lt;&lt; 16 ) ), \</span></div>
<div class="line"><span class="preprocessor">                                                  }</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* IPv6 Multicast group address. Change it to the mutlicast group address you want to join. */</span></div>
<div class="line"><span class="preprocessor">#define IPV6_MULTICAST_GROUP_ADDRESS              MAKE_IPV6_ADDRESS( 0XFF02, 0, 0, 0, 0, 0, 0, 0XFB)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* UDP Port. Change it to the required port number. */</span></div>
<div class="line"><span class="preprocessor">#define UDP_PORT                                  (50007)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* IPv6 address of your device. Change it to the IPv6 address of your device.*/</span></div>
<div class="line"><span class="preprocessor">#define IPV6_DEVICE_ADDRESS                       MAKE_IPV6_ADDRESS( 0xFE80, 0, 0, 0, 0x29D, 0x6BFF, 0xFE9D, 0xE2B4)</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> snippet_udp_ipv6_multicast_join_leave()</div>
<div class="line">{</div>
<div class="line">    <a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> handle;</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    uint32_t bytes_sent = 0;</div>
<div class="line">    uint32_t bytes_received = 0;</div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__ip__mreq__t.html">cy_socket_ip_mreq_t</a> imr;</div>
<div class="line"> </div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to hold the data to be sent to the multicast group. */</span></div>
<div class="line">    <span class="keywordtype">char</span> tx_buffer[] = <span class="stringliteral">&quot;Message to multicast group&quot;</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to store the incoming message from the multicast group. */</span></div>
<div class="line">    <span class="keywordtype">char</span> rx_buffer[MAX_UDP_RECV_BUFFER_SIZE];</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/*</span></div>
<div class="line"><span class="comment">     * Add the code to obtain the IPv4 address of the device.</span></div>
<div class="line"><span class="comment">     */</span></div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* IPv6 address of the device. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__ip__address__t.html">cy_socket_ip_address_t</a> ip_addr = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#af849e54b89b4412aaca8eb46275f2b4b">v6</a> = IPV6_DEVICE_ADDRESS, <span class="comment">/* Attention: This IP address is hardcoded with random address in this snippet. It should be updated with the actual device IPv4 address. */</span></div>
<div class="line">        .version = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789daa5efbdd596c434140bed0f8f0d5d9582">CY_SOCKET_IP_VER_V6</a></div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* IPV6 address of the multicast group. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__ip__address__t.html">cy_socket_ip_address_t</a> multicast_address = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#af849e54b89b4412aaca8eb46275f2b4b">v6</a> = IPV6_MULTICAST_GROUP_ADDRESS,</div>
<div class="line">        .version = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789daa5efbdd596c434140bed0f8f0d5d9582">CY_SOCKET_IP_VER_V6</a></div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Fill the multicast request variable. */</span></div>
<div class="line">    imr.<a class="code hl_variable" href="structcy__socket__ip__mreq__t.html#af355ca415d98f381b1e39d95a3741acc">if_addr</a> = ip_addr;</div>
<div class="line">    imr.<a class="code hl_variable" href="structcy__socket__ip__mreq__t.html#afbd0341c76b9026737ee7db33237fb8e">multi_addr</a> = multicast_address;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a UDP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gaa2d04e27350109a0084d8ea90eb24a27">CY_SOCKET_DOMAIN_AF_INET6</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga9ea5c6f97612e469393306ddefc38657">CY_SOCKET_TYPE_DGRAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga2bbff715a00d6ce5ff669b12a75c7441">CY_SOCKET_IPPROTO_UDP</a>, &amp;handle);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Join an IPv6 multicast group. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga6158e1e598a55be5e42d5db5f921d13e">CY_SOCKET_SOL_IP</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga6eb3fccf457611f775c37e58442449b3">CY_SOCKET_SO_JOIN_MULTICAST_GROUP</a>, &amp;imr, <span class="keyword">sizeof</span>(imr));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Socket address of the multicast group. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> socket_address = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a> = multicast_address,</div>
<div class="line">        .port = UDP_PORT</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Bind the UDP socket created to the multicast address and port. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gac05772b8936d96ec8d8367b661912608">cy_socket_bind</a>(handle, &amp;socket_address, <span class="keyword">sizeof</span>(socket_address));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Send a message to the multicast group. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga4bc4e8aed9916206f073cb22cf4009cc">cy_socket_sendto</a>(handle, tx_buffer, strlen(tx_buffer), <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, &amp;socket_address, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a>), &amp;bytes_sent);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Receive a message from the multicast group. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga29df5cbdaa9de0f324cf947454577727">cy_socket_recvfrom</a>(handle, rx_buffer, MAX_UDP_RECV_BUFFER_SIZE, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, NULL, NULL, &amp;bytes_received);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Leave the multicast group. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga6158e1e598a55be5e42d5db5f921d13e">CY_SOCKET_SOL_IP</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaa3b9e9b88b1affecbb653fb1375771e3">CY_SOCKET_SO_LEAVE_MULTICAST_GROUP</a>, &amp;imr, <span class="keyword">sizeof</span>(imr));</div>
<div class="line">}</div>
</div><!-- fragment --><h2><a class="anchor" id="snip11"></a>
Code Snippet 11: Enable broadcast messages - Enable broadcast messages, send a broadcast packet, and receive a broadcast packet.</h2>
<p >This code snippet demonstrates how to enable broadcast messages, and send and receive a broadcast packet. </p><div class="fragment"><div class="line"> </div>
<div class="line"><span class="preprocessor">#define MAKE_IPV4_ADDRESS(a, b, c, d)     ((((uint32_t) d) &lt;&lt; 24) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  c) &lt;&lt; 16) | \</span></div>
<div class="line"><span class="preprocessor">                                          (((uint32_t)  b) &lt;&lt; 8) |\</span></div>
<div class="line"><span class="preprocessor">                                          ((uint32_t)   a))</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Broadcast address. */</span></div>
<div class="line"><span class="preprocessor">#define BROADCAST_ADDRESS                 MAKE_IPV4_ADDRESS(255, 255, 255, 255)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* IP address to bind the socket for any incoming interface. */</span></div>
<div class="line"><span class="preprocessor">#define IP_ADDRESS_ANY                    MAKE_IPV4_ADDRESS(0, 0, 0, 0)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* UDP Port number. Change it to the required port number. */</span></div>
<div class="line"><span class="preprocessor">#define UDP_PORT                          (50007)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Buffer size to store the incoming broadcast message. */</span></div>
<div class="line"><span class="preprocessor">#define MAX_UDP_RECV_BUFFER_SIZE          (20)</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> snippet_enable_broadcast_messages()</div>
<div class="line">{</div>
<div class="line">    <a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> handle;</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    uint32_t bytes_sent = 0;</div>
<div class="line">    uint32_t bytes_received = 0;</div>
<div class="line">    uint8_t broadcast_enable = 1;</div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to hold the data to be sent to the multicast group. */</span></div>
<div class="line">    <span class="keywordtype">char</span> tx_buffer[] = <span class="stringliteral">&quot;Broadcasting Message&quot;</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Buffer to store the incoming broadcast message. */</span></div>
<div class="line">    <span class="keywordtype">char</span> rx_buffer[MAX_UDP_RECV_BUFFER_SIZE];</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Broadcast socket address. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> socket_address = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a> = BROADCAST_ADDRESS,</div>
<div class="line">        .ip_address.version = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a>,</div>
<div class="line">        .port = UDP_PORT</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Socket address to bind the socket to. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> socket_address_any = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a> = IP_ADDRESS_ANY,</div>
<div class="line">        .ip_address.version = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a>,</div>
<div class="line">        .port = UDP_PORT</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a UDP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga9ea5c6f97612e469393306ddefc38657">CY_SOCKET_TYPE_DGRAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga2bbff715a00d6ce5ff669b12a75c7441">CY_SOCKET_IPPROTO_UDP</a>, &amp;handle);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Enable broadcast messages. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaf36b6f0f1bc53927d148746fedad7149">CY_SOCKET_SOL_SOCKET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gaed20b264dc3c370f3136ef8923f5ad89">CY_SOCKET_SO_BROADCAST</a>, (<span class="keywordtype">void</span>*)(&amp;broadcast_enable), <span class="keyword">sizeof</span>(broadcast_enable));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Bind the UDP socket created to the broadcast address and port. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gac05772b8936d96ec8d8367b661912608">cy_socket_bind</a>(handle, &amp;socket_address_any, <span class="keyword">sizeof</span>(socket_address_any));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Send a broadcast message. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga4bc4e8aed9916206f073cb22cf4009cc">cy_socket_sendto</a>(handle, tx_buffer, strlen(tx_buffer), <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, &amp;socket_address, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a>), &amp;bytes_sent);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Receive a broadcast message. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga29df5cbdaa9de0f324cf947454577727">cy_socket_recvfrom</a>(handle, rx_buffer, MAX_UDP_RECV_BUFFER_SIZE, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga91444208828cd6b96895d64b879513d4">CY_SOCKET_FLAGS_NONE</a>, NULL, NULL, &amp;bytes_received);</div>
<div class="line">}</div>
<div class="ttc" id="agroup__group__secure__sockets__macros_html_gaed20b264dc3c370f3136ef8923f5ad89"><div class="ttname"><a href="group__group__secure__sockets__macros.html#gaed20b264dc3c370f3136ef8923f5ad89">CY_SOCKET_SO_BROADCAST</a></div><div class="ttdeci">#define CY_SOCKET_SO_BROADCAST</div><div class="ttdoc">Enable/disable sending and receiving broadcast messages on the socket.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets_constants.h:332</div></div>
</div><!-- fragment --><h2><a class="anchor" id="snip12"></a>
Code Snippet 12: Get the current time from the NTP server - Send an NTP request packet to the NTP server, and receive an NTP reply packet.</h2>
<p >This code snippet demonstrates how to get the current time from the NTP server using a UDP socket. </p><div class="fragment"><div class="line"> </div>
<div class="line"><span class="comment">/* Converts a 32-bit value from network byte order to host byte order. */</span></div>
<div class="line"><span class="preprocessor">#define NTOHL(x)    ((((x) &amp; (uint32_t)0x000000ffUL) &lt;&lt; 24) | \</span></div>
<div class="line"><span class="preprocessor">                     (((x) &amp; (uint32_t)0x0000ff00UL) &lt;&lt;  8) | \</span></div>
<div class="line"><span class="preprocessor">                     (((x) &amp; (uint32_t)0x00ff0000UL) &gt;&gt;  8) | \</span></div>
<div class="line"><span class="preprocessor">                     (((x) &amp; (uint32_t)0xff000000UL) &gt;&gt; 24))</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#define NTP_SERVER                                  &quot;pool.ntp.org&quot;</span></div>
<div class="line"><span class="comment">/* UDP port number of the NTP server. */</span></div>
<div class="line"><span class="preprocessor">#define NTP_SERVER_PORT_NUM                         (123)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* NTP epoch time.*/</span></div>
<div class="line"><span class="preprocessor">#define NTP_EPOCH                                   (86400U * (365U * 70U + 17U))</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* LI: leap indicator. */</span></div>
<div class="line"><span class="preprocessor">#define NTP_LEAP_INDICATOR_CLOCK_UNSYNCRONIZED      (3)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* VN: version number. */</span></div>
<div class="line"><span class="preprocessor">#define NTP_VERSION_NUM_V3                          (3)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Mode. */</span></div>
<div class="line"><span class="preprocessor">#define NTP_MODE_CLIENT                             (3)</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment"> * NTP packet structure, taken from RFC 1305</span></div>
<div class="line"><span class="comment"> * http://www.ietf.org/rfc/rfc1305.txt</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span></div>
<div class="line">{</div>
<div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> mode : 3;</div>
<div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> vn   : 3;</div>
<div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> li   : 2;</div>
<div class="line">    uint8_t      stratum;</div>
<div class="line">    int8_t       poll;</div>
<div class="line">    uint8_t      precision;</div>
<div class="line">    uint32_t     root_delay;</div>
<div class="line">    uint32_t     root_dispersion;</div>
<div class="line">    uint32_t     reference_identifier;</div>
<div class="line">    uint32_t     reference_timestamp_seconds;</div>
<div class="line">    uint32_t     reference_timestamp_fraction;</div>
<div class="line">    uint32_t     originate_timestamp_seconds;</div>
<div class="line">    uint32_t     originate_timestamp_fraction;</div>
<div class="line">    uint32_t     receive_timestamp_seconds;</div>
<div class="line">    uint32_t     receive_timestamp_fraction;</div>
<div class="line">    uint32_t     transmit_timestamp_seconds;</div>
<div class="line">    uint32_t     transmit_timestamp_fraction;</div>
<div class="line">} ntp_packet_t;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> snippet_get_ntp_time()</div>
<div class="line">{</div>
<div class="line">    <a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> handle;</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    uint32_t bytes_sent = 0;</div>
<div class="line">    uint32_t bytes_received = 0;</div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__ip__address__t.html">cy_socket_ip_address_t</a> server_ip_addr;</div>
<div class="line">    uint32_t seconds;</div>
<div class="line">    (void)(result);</div>
<div class="line">    (void)(seconds);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* NTP Request packet. */</span></div>
<div class="line">    ntp_packet_t ntp_request;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* NTP Reply packet. */</span></div>
<div class="line">    ntp_packet_t ntp_reply;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Get the IP address of the NTP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gaeb05bcbd775c36f5fe3fe9d676329494">cy_socket_gethostbyname</a>(NTP_SERVER, <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a>, &amp;server_ip_addr);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Server socket address. */</span></div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> socket_address = {</div>
<div class="line">        .<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a> = server_ip_addr,</div>
<div class="line">        .port = NTP_SERVER_PORT_NUM</div>
<div class="line">    };</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a UDP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga9ea5c6f97612e469393306ddefc38657">CY_SOCKET_TYPE_DGRAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga2bbff715a00d6ce5ff669b12a75c7441">CY_SOCKET_IPPROTO_UDP</a>, &amp;handle);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the NTP request packet to be sent to the server. */</span></div>
<div class="line">    memset(&amp;ntp_request, 0, <span class="keyword">sizeof</span>(ntp_packet_t));</div>
<div class="line">    ntp_request.li   = NTP_LEAP_INDICATOR_CLOCK_UNSYNCRONIZED;</div>
<div class="line">    ntp_request.vn   = NTP_VERSION_NUM_V3;</div>
<div class="line">    ntp_request.mode = NTP_MODE_CLIENT;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Send the NTP request packet to the server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga4bc4e8aed9916206f073cb22cf4009cc">cy_socket_sendto</a>(handle, &amp;ntp_request, <span class="keyword">sizeof</span>(ntp_request), 0, &amp;socket_address, (uint32_t)<span class="keyword">sizeof</span>(socket_address), &amp;bytes_sent);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Receive the reply packet from the NTP server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga29df5cbdaa9de0f324cf947454577727">cy_socket_recvfrom</a>(handle, &amp;ntp_reply, <span class="keyword">sizeof</span>(ntp_reply), 0, NULL, NULL, &amp;bytes_received);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Convert to January 1, 1970 epoch time in seconds. */</span></div>
<div class="line">    seconds = NTOHL(ntp_reply.transmit_timestamp_seconds) - NTP_EPOCH;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Delete the socket. */</span></div>
<div class="line">    <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga3c811410848871c3c4b0721a3b37dc24">cy_socket_delete</a>(handle);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* De-initialize the Secure Socket Library. */</span></div>
<div class="line">    <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga0eb3e8e31c5a1c11c08ac35807bb5680">cy_socket_deinit</a>();</div>
<div class="line">}</div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_ga0eb3e8e31c5a1c11c08ac35807bb5680"><div class="ttname"><a href="group__group__secure__sockets__functions.html#ga0eb3e8e31c5a1c11c08ac35807bb5680">cy_socket_deinit</a></div><div class="ttdeci">cy_rslt_t cy_socket_deinit(void)</div><div class="ttdoc">Releases the resources allocated in cy_socket_init function.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_ga3c811410848871c3c4b0721a3b37dc24"><div class="ttname"><a href="group__group__secure__sockets__functions.html#ga3c811410848871c3c4b0721a3b37dc24">cy_socket_delete</a></div><div class="ttdeci">cy_rslt_t cy_socket_delete(cy_socket_t handle)</div><div class="ttdoc">Releases the resources allocated for the socket by the cy_socket_create function.</div></div>
<div class="ttc" id="agroup__group__secure__sockets__functions_html_gaeb05bcbd775c36f5fe3fe9d676329494"><div class="ttname"><a href="group__group__secure__sockets__functions.html#gaeb05bcbd775c36f5fe3fe9d676329494">cy_socket_gethostbyname</a></div><div class="ttdeci">cy_rslt_t cy_socket_gethostbyname(const char *hostname, cy_socket_ip_version_t ip_ver, cy_socket_ip_address_t *addr)</div><div class="ttdoc">Resolves a host name using Domain Name Service.</div></div>
</div><!-- fragment --><h2><a class="anchor" id="snip13"></a>
Code Snippet 13: Connect to secure server in PKCS mode - TLS connection using PKCS mode for secured kit (CY8CKIT-064S0S2-4343W)</h2>
<p >This code snippet demonstrates establishing a TLS (secure TCP) connection using a Secure PSoC6 kit with PKCS mode. NOTE: The device credentials and RootCA certificates should be provisioned in the PSoC6 secure hardware before running this snippet. </p><div class="fragment"><div class="line"> </div>
<div class="line"><span class="preprocessor">#ifdef CY_SECURE_SOCKETS_PKCS_SUPPORT</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#define PORT       443</span></div>
<div class="line"><span class="preprocessor">#define HOSTNAME   &quot;www.httpbin.org&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#ifdef CY_TFM_PSA_SUPPORTED</span></div>
<div class="line"><span class="keyword">static</span> <span class="keyword">struct  </span>ns_mailbox_queue_t ns_mailbox_queue;</div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> tfm_ns_multi_core_boot(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line">    int32_t ret;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">if</span> (tfm_ns_wait_for_s_cpu_ready())</div>
<div class="line">    {</div>
<div class="line">        <span class="comment">/* Error sync&#39;ing with secure core */</span></div>
<div class="line">        <span class="comment">/* Avoid undefined behavior after multi-core sync-up failed */</span></div>
<div class="line">        <span class="keywordflow">for</span> (;;)</div>
<div class="line">        {</div>
<div class="line">        }</div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">    ret = tfm_ns_mailbox_init(&amp;ns_mailbox_queue);</div>
<div class="line">    <span class="keywordflow">if</span> (ret != MAILBOX_SUCCESS)</div>
<div class="line">    {</div>
<div class="line">        <span class="comment">/* Non-secure mailbox initialization failed. */</span></div>
<div class="line">        <span class="comment">/* Avoid undefined behavior after NS mailbox initialization failed */</span></div>
<div class="line">        <span class="keywordflow">for</span> (;;)</div>
<div class="line">        {</div>
<div class="line">        }</div>
<div class="line">    }</div>
<div class="line">}</div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> snippet_secure_sockets_tls_connect_pkcs()</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* TCP socket handle */</span></div>
<div class="line">    <a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> socket_handle;</div>
<div class="line"> </div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> address;</div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__ip__address__t.html">cy_socket_ip_address_t</a> ip_addr;</div>
<div class="line"> </div>
<div class="line">    cy_nw_ip_address_t nw_ip_addr;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordtype">char</span> buf[16];</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#ifdef CY_TFM_PSA_SUPPORTED</span></div>
<div class="line">    tfm_ns_multi_core_boot();</div>
<div class="line">    <span class="comment">/* Initialize TFM interface */</span></div>
<div class="line">    tfm_ns_interface_init();</div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* TLS authentication mode. Setting the CY_SOCKET_TLS_VERIFY_REQUIRED authentication mode</span></div>
<div class="line"><span class="comment">    requires a successful TLS handshake in order to establish a secure socket communication.*/</span></div>
<div class="line">    <a class="code hl_enumeration" href="group__group__secure__sockets__enums.html#gaf0599f554a1154ae83b4f587520fa705">cy_socket_tls_auth_mode_t</a> tls_auth_mode = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#ggaf0599f554a1154ae83b4f587520fa705a68f0cd442bb9f64416a6351f6d8c5a78">CY_SOCKET_TLS_VERIFY_REQUIRED</a>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Resolve hostname to IP address */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gaeb05bcbd775c36f5fe3fe9d676329494">cy_socket_gethostbyname</a>(HOSTNAME, <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a>, &amp;ip_addr);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Print IPv4 address */</span></div>
<div class="line">    nw_ip_addr.version = NW_IP_IPV4;</div>
<div class="line">    nw_ip_addr.ip.v4 = ip_addr.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a>;</div>
<div class="line"> </div>
<div class="line">    cy_nw_ntoa(&amp;nw_ip_addr, buf);</div>
<div class="line">    printf(<span class="stringliteral">&quot;IP Address : %s \n&quot;</span>, buf);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a new secure TCP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gac0610ba6e8fe01ccbaacf89145f9285a">CY_SOCKET_TYPE_STREAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga1114a6ecf098cf499e2c9ad3d651efc3">CY_SOCKET_IPPROTO_TLS</a>, &amp;socket_handle);</div>
<div class="line"> </div>
<div class="line">    address.<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a> = ip_addr.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a>;</div>
<div class="line">    address.<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#adff8c23c18f451bc833136c9b39762ca">version</a> = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a>;</div>
<div class="line">    address.<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#a78df7a67a185b4114913f8e983a3a5d0">port</a> = PORT;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Set the TLS authentication mode. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(socket_handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga87878a7761a845f17f24f5e52c73f61f">CY_SOCKET_SOL_TLS</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga9ce4ab8fca2ff7306f05f7433835f394">CY_SOCKET_SO_TLS_AUTH_MODE</a>, &amp;tls_auth_mode, <span class="keyword">sizeof</span>(<a class="code hl_enumeration" href="group__group__secure__sockets__enums.html#gaf0599f554a1154ae83b4f587520fa705">cy_socket_tls_auth_mode_t</a>));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Connect to server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga2e07189d60c7e6a130302d2e2db19653">cy_socket_connect</a>(socket_handle, &amp;address, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a>));</div>
<div class="line">}</div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="ttc" id="astructcy__socket__ip__address__t_html_adff8c23c18f451bc833136c9b39762ca"><div class="ttname"><a href="structcy__socket__ip__address__t.html#adff8c23c18f451bc833136c9b39762ca">cy_socket_ip_address_t::version</a></div><div class="ttdeci">cy_socket_ip_version_t version</div><div class="ttdoc">IP version cy_socket_ip_version_t.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:144</div></div>
<div class="ttc" id="astructcy__socket__sockaddr__t_html_a78df7a67a185b4114913f8e983a3a5d0"><div class="ttname"><a href="structcy__socket__sockaddr__t.html#a78df7a67a185b4114913f8e983a3a5d0">cy_socket_sockaddr_t::port</a></div><div class="ttdeci">uint16_t port</div><div class="ttdoc">Port Number in host byte order.</div><div class="ttdef"><b>Definition:</b> cy_secure_sockets.h:158</div></div>
</div><!-- fragment --><h2><a class="anchor" id="snip14"></a>
Code Snippet 14: Connect to secure server in PKCS mode - TLS connection using PKCS mode for secured kit (CYW955913EVK-01)</h2>
<p >This code snippet demonstrates establishing a TLS (secure TCP) connection using a CYW955913EVK-01 kit with PKCS mode. NOTE : The Private key will be stored in the secure element of the device in the PKCS mode. </p><div class="fragment"><div class="line"> </div>
<div class="line"><span class="preprocessor">#ifdef CY_SECURE_SOCKETS_PKCS_SUPPORT</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#define PORT       443</span></div>
<div class="line"><span class="preprocessor">#define HOSTNAME   &quot;www.httpbin.org&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Update keyCLIENT_CERTIFICATE_PEM with the device certificate generated */</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span> keyCLIENT_CERTIFICATE_PEM[] =</div>
<div class="line"><span class="stringliteral">&quot;-----BEGIN CERTIFICATE-----\n&quot;</span></div>
<div class="line"><span class="stringliteral">&quot;-----END CERTIFICATE-----\n&quot;</span>;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> snippet_secure_sockets_tls_connect_pkcs()</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result;</div>
<div class="line">    (void)(result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* TCP socket handle */</span></div>
<div class="line">    <a class="code hl_typedef" href="group__group__secure__sockets__typedefs.html#ga12b5aa294d6c930ba50ab730d39ed511">cy_socket_t</a> socket_handle;</div>
<div class="line"> </div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a> address;</div>
<div class="line">    <a class="code hl_struct" href="structcy__socket__ip__address__t.html">cy_socket_ip_address_t</a> ip_addr;</div>
<div class="line"> </div>
<div class="line">    cy_nw_ip_address_t nw_ip_addr;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordtype">void</span> *tls_identity = NULL;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordtype">char</span> buf[16];</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* TLS authentication mode. Setting the CY_SOCKET_TLS_VERIFY_REQUIRED authentication mode</span></div>
<div class="line"><span class="comment">    requires a successful TLS handshake in order to establish a secure socket communication.*/</span></div>
<div class="line">    <a class="code hl_enumeration" href="group__group__secure__sockets__enums.html#gaf0599f554a1154ae83b4f587520fa705">cy_socket_tls_auth_mode_t</a> tls_auth_mode = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#ggaf0599f554a1154ae83b4f587520fa705a68f0cd442bb9f64416a6351f6d8c5a78">CY_SOCKET_TLS_VERIFY_REQUIRED</a>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Initialize the Secure Sockets Library. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga49a49b4e3b4293aa3855f47ce831ebe2">cy_socket_init</a>();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Resolve hostname to IP address */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#gaeb05bcbd775c36f5fe3fe9d676329494">cy_socket_gethostbyname</a>(HOSTNAME, <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a>, &amp;ip_addr);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Print IPv4 address */</span></div>
<div class="line">    nw_ip_addr.version = NW_IP_IPV4;</div>
<div class="line">    nw_ip_addr.ip.v4 = ip_addr.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a>;</div>
<div class="line"> </div>
<div class="line">    cy_nw_ntoa(&amp;nw_ip_addr, buf);</div>
<div class="line">    printf(<span class="stringliteral">&quot;IP Address : %s \n&quot;</span>, buf);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Create a new secure TCP socket. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>(<a class="code hl_define" href="group__group__secure__sockets__macros.html#gacc301fb73845cb915cfacffc1b7d0c23">CY_SOCKET_DOMAIN_AF_INET</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gac0610ba6e8fe01ccbaacf89145f9285a">CY_SOCKET_TYPE_STREAM</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga1114a6ecf098cf499e2c9ad3d651efc3">CY_SOCKET_IPPROTO_TLS</a>, &amp;socket_handle);</div>
<div class="line"> </div>
<div class="line">    address.<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a> = ip_addr.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a98f8abed868445e1a8348da559d4f574">ip</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#a1b68b61521bd0b095ed35bea52f6fd82">v4</a>;</div>
<div class="line">    address.<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#ac4caf040376b30c85ca9065a20d5616c">ip_address</a>.<a class="code hl_variable" href="structcy__socket__ip__address__t.html#adff8c23c18f451bc833136c9b39762ca">version</a> = <a class="code hl_enumvalue" href="group__group__secure__sockets__enums.html#gga97cb501a58195859467b126cf2a2789da32785b59f763a33cac58932132a90520">CY_SOCKET_IP_VER_V4</a>;</div>
<div class="line">    address.<a class="code hl_variable" href="structcy__socket__sockaddr__t.html#a78df7a67a185b4114913f8e983a3a5d0">port</a> = PORT;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Pass private_key  NULL and private_key_len 0 */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__cy__tls__functions.html#ga3d2408681bbcea3b869cb5ae21cd0d5e">cy_tls_create_identity</a>(keyCLIENT_CERTIFICATE_PEM, strlen(keyCLIENT_CERTIFICATE_PEM), NULL, 0, (<span class="keywordtype">void</span>**)&amp;tls_identity);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Set the TCP socket to use the TLS identity. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(socket_handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga87878a7761a845f17f24f5e52c73f61f">CY_SOCKET_SOL_TLS</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#gafa0ac839774a33feeb037dbb0c449cc3">CY_SOCKET_SO_TLS_IDENTITY</a>,</div>
<div class="line">                                  tls_identity, <span class="keyword">sizeof</span>((uint32_t)tls_identity));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Set the TLS authentication mode. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt</a>(socket_handle, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga87878a7761a845f17f24f5e52c73f61f">CY_SOCKET_SOL_TLS</a>, <a class="code hl_define" href="group__group__secure__sockets__macros.html#ga9ce4ab8fca2ff7306f05f7433835f394">CY_SOCKET_SO_TLS_AUTH_MODE</a>, &amp;tls_auth_mode, <span class="keyword">sizeof</span>(<a class="code hl_enumeration" href="group__group__secure__sockets__enums.html#gaf0599f554a1154ae83b4f587520fa705">cy_socket_tls_auth_mode_t</a>));</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/* Connect to server. */</span></div>
<div class="line">    result = <a class="code hl_function" href="group__group__secure__sockets__functions.html#ga2e07189d60c7e6a130302d2e2db19653">cy_socket_connect</a>(socket_handle, &amp;address, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="structcy__socket__sockaddr__t.html">cy_socket_sockaddr_t</a>));</div>
<div class="line">}</div>
<div class="line"><span class="preprocessor">#endif</span></div>
</div><!-- fragment --><h1><a class="anchor" id="section_troubleshoot"></a>
Troubleshooting</h1>
<h2><a class="anchor" id="Problem1"></a>
Problem1: cy_socket_listen() fails if called on more than one socket.</h2>
<p ><b>Description:</b> The default lwIP configuration provided by the Middleware Core library defines the MEMP_NUM_TCP_PCB_LISTEN option to 1; therefore, if <a class="el" href="group__group__secure__sockets__functions.html#ga1d5c6e81830f6c81f820e02f6d0cf6a0">cy_socket_listen()</a> is called for the second socket, the function fails with the error code <a class="el" href="group__group__secure__sockets__results.html#ga7631cf4765aea807ee7eef6628ea962c">CY_RSLT_MODULE_SECURE_SOCKETS_NOMEM</a>. <br  />
 <b>Solution:</b> Change MEMP_NUM_TCP_PCB_LISTEN value in lwipopts.h to support more than one server socket.</p>
<h2><a class="anchor" id="Problem2"></a>
Problem2: Sometimes, the cy_socket_recv() function does not return for 10 seconds.</h2>
<p ><b>Description:</b> <a class="el" href="group__group__secure__sockets__functions.html#gaeb1269bd36384293539dc749095412f7">cy_socket_recv()</a> returns after 10 seconds if the requested number of bytes is not available in the socket. This happens because the default receive timeout is configured as 10 seconds when the socket is created. <br  />
 <b>Solution:</b> Change the default receive timeout value for the socket by calling the <a class="el" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt()</a> function with the <a class="el" href="group__group__secure__sockets__macros.html#ga1fcbd0b7ab0d808a7d0a88eaffe04e00">CY_SOCKET_SO_RCVTIMEO</a> socket option with the required timeout value.</p>
<h2><a class="anchor" id="Problem3"></a>
Problem3: TCP packets are dropped.</h2>
<p ><b>Description:</b> When an application is consuming data at a lower rate, the network stack continues to receive the data at the rate it arrives from the peer. At some point, the network stack's receive queue can overflow causing packets to get dropped. <br  />
 <b>Solution:</b> Applications consuming data at a lower rate should adjust the TCP receive queue size and TCP Window size so that when the receive queue size becomes full, the TCP window size also becomes zero. To change the TCP receive queue size, change the DEFAULT_TCP_RECVMBOX_SIZE macro value in the lwipopts.h file. To change the TCP Window size, define the TCP_WND macro in lwipopts.h to override the default value defined the opt.h file.</p>
<h2><a class="anchor" id="Problem4"></a>
Problem4: cy_socket_create() function fails with the CY_RSLT_MODULE_SECURE_SOCKETS_NOMEM error while creating multiple sockets.</h2>
<p ><b>Description:</b> The maximum number of TCP and UDP sockets that can be created depends on the network stack configuration. Trying to create more than the maximum value configured can cause the <a class="el" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create()</a> function to return the error <a class="el" href="group__group__secure__sockets__results.html#ga7631cf4765aea807ee7eef6628ea962c">CY_RSLT_MODULE_SECURE_SOCKETS_NOMEM</a>. <br  />
 <b>Solution:</b> Configure the maximum number of TCP and UDP sockets to be supported by changing the MEMP_NUM_TCP_PCB and MEMP_NUM_UDP_PCB values respectively in the lwipopts.h file. Also note that the total number of TCP and UDP configurations should not exceed the value of the MEMP_NUM_NETCONN macro defined in the lwipopts.h file.</p>
<h2><a class="anchor" id="Problem5"></a>
Problem5: An existing socket cannot be reused to establish a new connection just with socket disconnect.</h2>
<p ><b>Description:</b> lwIP does not support re-connect on a disconnected socket. Once a connected TCP socket is disconnected, it cannot be reused for a new connection or re-connection. <br  />
 <b>Solution:</b> Delete the disconnected socket and create a new socket using <a class="el" href="group__group__secure__sockets__functions.html#ga30bbbf20a277e2a7e622df504b014a4b">cy_socket_create</a>, and try to connect with the new socket using <a class="el" href="group__group__secure__sockets__functions.html#ga2e07189d60c7e6a130302d2e2db19653">cy_socket_connect</a>.</p>
<h2><a class="anchor" id="Problem6"></a>
Problem6: cy_socket_sendto() with multicast destination addresses fails on the SoftAP interface.</h2>
<p ><b>Description:</b> If a socket is used for multicast operations over the SoftAP interface without binding it to the SoftAP interface, <a class="el" href="group__group__secure__sockets__functions.html#ga4bc4e8aed9916206f073cb22cf4009cc">cy_socket_sendto()</a> fails with the CY_RSLT_MODULE_SECURE_SOCKETS_ERROR_ROUTING error. <br  />
 <b>Solution:</b> Bind the socket to the SoftAP interface using the <a class="el" href="group__group__secure__sockets__functions.html#ga02e453d715d77075c59d751f6b07ae8f">cy_socket_setsockopt()</a> function with the <a class="el" href="group__group__secure__sockets__macros.html#ga9bcc73d3e5fd22f8cceb98f57507703d">CY_SOCKET_SO_BINDTODEVICE</a> socket option. </p>
</div></div><!-- PageDoc -->
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath">
    <ul>
        <li class="footer">
            Generated for <b>Secure Sockets</b> by <b>Cypress Semiconductor Corporation</b>.
            All rights reserved.
        </li>
    </ul>
</div>
</body>
</html>
